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