Spaces:
Sleeping
Sleeping
File size: 2,597 Bytes
af8cdac c7e0507 24b6da8 c594ee2 af8cdac c7e0507 24b6da8 5d29fa6 24b6da8 32173b7 af8cdac 32173b7 af8cdac c7e0507 32173b7 c7e0507 32173b7 c7e0507 32173b7 af8cdac c7e0507 af8cdac 32173b7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import streamlit as st
import pandas as pd
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
import seaborn as sns
from fields.likert_fields import likert_fields
@st.cache_data
def show(df):
# Load the Chinese font
chinese_font = FontProperties(fname='notosans.ttf', size=12)
st.title("Investing")
st.markdown(
f"<h2 style='text-align: center;'>Investing Experience (Overall)</h2>", unsafe_allow_html=True)
show_investment_count(df, chinese_font)
st.markdown(
f"<h2 style='text-align: center;'>Choice Experiment</h2>", unsafe_allow_html=True)
visualize_investment_data(df, chinese_font)
def show_investment_count(df, chinese_font):
# Count the number of people who have invested and who have not
investment_count = df["你/妳覺得目前有任何投資嗎?"].value_counts().reset_index()
investment_count.columns = ['Investment', 'Count']
# Create a bar chart using seaborn
plt.figure(figsize=(10, 6))
barplot = sns.barplot(x='Investment', y='Count', data=investment_count, palette='viridis')
ax = plt.gca() # Get the current Axes instance on the current figure matching the given keyword args, or create one.
ax.set_xticklabels(ax.get_xticklabels(), fontproperties=chinese_font)
# Add labels and title
plt.xlabel('Do you currently have any investment?', fontsize=12, fontproperties=chinese_font)
plt.ylabel('Count', fontsize=12, fontproperties=chinese_font)
plt.title("Number of People Who Have/Haven't Invested", fontsize=16, fontproperties=chinese_font)
# Display values on the bars
for index, value in enumerate(investment_count['Count']):
plt.text(index, value, str(value), ha='center', va='bottom', fontproperties=chinese_font)
# Display the chart in Streamlit
st.pyplot(plt)
def visualize_investment_data(df, chinese_font):
# Field related to investment choices
investment_field = "你/妳選哪個投資?"
title = "Investment Choices"
# Summarize the data
investment_data = df[investment_field].value_counts().head(100) # Adjust the number as needed
# Plot the data
plt.figure(figsize=(10, 6))
investment_data.plot(kind='bar', color='skyblue')
plt.title(title, fontproperties=chinese_font)
plt.xlabel('Investment Options', fontproperties=chinese_font)
plt.ylabel('Number of Responses', fontproperties=chinese_font)
plt.xticks(rotation=45, ha='right', fontproperties=chinese_font)
plt.tight_layout()
# Display plot in Streamlit
st.pyplot(plt) |