Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -4,6 +4,10 @@ import pandas as pd
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import matplotlib.ticker as ticker
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Load the data
|
| 8 |
df = pd.read_csv("data.csv")
|
| 9 |
|
|
@@ -25,18 +29,19 @@ def plot_data(selected_entities, year_range, show_raw_data):
|
|
| 25 |
fig, ax = plt.subplots(figsize=(12, 7))
|
| 26 |
for entity in selected_entities:
|
| 27 |
entity_data = filtered_df[filtered_df["Entity"] == entity]
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
ax.set_xlabel("
|
| 31 |
-
ax.set_ylabel("
|
| 32 |
-
ax.set_title("Top 5%
|
| 33 |
-
ax.legend(title="
|
| 34 |
ax.grid(True, linestyle=":", alpha=0.7)
|
| 35 |
ax.set_facecolor("#f5f5f5") # Light grey background
|
| 36 |
|
| 37 |
# Improve x-axis ticks
|
| 38 |
ax.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
|
| 39 |
-
plt.xticks(rotation=45, ha=
|
| 40 |
|
| 41 |
plt.tight_layout() # Adjust layout to prevent labels from overlapping
|
| 42 |
|
|
@@ -47,15 +52,15 @@ def plot_data(selected_entities, year_range, show_raw_data):
|
|
| 47 |
return fig, pd.DataFrame()
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
-
with gr.Blocks() as demo:
|
| 51 |
-
gr.Markdown("# Top 5%
|
| 52 |
-
gr.Markdown("
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
-
with gr.Column():
|
| 56 |
entity_selector = gr.CheckboxGroup(
|
| 57 |
choices=entities,
|
| 58 |
-
label="
|
| 59 |
value=["Australia", "China", "Germany", "Japan", "United States"]
|
| 60 |
)
|
| 61 |
year_slider = gr.Slider(
|
|
@@ -63,14 +68,17 @@ with gr.Blocks() as demo:
|
|
| 63 |
maximum=latest_year,
|
| 64 |
value=[earliest_year, latest_year],
|
| 65 |
step=1,
|
| 66 |
-
label="
|
| 67 |
)
|
| 68 |
-
show_raw_data_checkbox = gr.Checkbox(label="
|
| 69 |
-
submit_button = gr.Button("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
output_dataframe = gr.DataFrame(label="原始数据")
|
| 74 |
|
| 75 |
submit_button.click(
|
| 76 |
fn=plot_data,
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import matplotlib.ticker as ticker
|
| 6 |
|
| 7 |
+
# Set font for CJK characters
|
| 8 |
+
plt.rcParams['font.sans-serif'] = ['Noto Sans CJK SC', 'DejaVu Sans']
|
| 9 |
+
plt.rcParams['axes.unicode_minus'] = False
|
| 10 |
+
|
| 11 |
# Load the data
|
| 12 |
df = pd.read_csv("data.csv")
|
| 13 |
|
|
|
|
| 29 |
fig, ax = plt.subplots(figsize=(12, 7))
|
| 30 |
for entity in selected_entities:
|
| 31 |
entity_data = filtered_df[filtered_df["Entity"] == entity]
|
| 32 |
+
if not entity_data.empty:
|
| 33 |
+
ax.plot(entity_data["Year"], entity_data["Percent"], label=entity, marker=".", markersize=8)
|
| 34 |
|
| 35 |
+
ax.set_xlabel("Year", fontsize=12)
|
| 36 |
+
ax.set_ylabel("Income Share (%)", fontsize=12)
|
| 37 |
+
ax.set_title("Top 5% Income Share Trend", fontsize=16, fontweight="bold")
|
| 38 |
+
ax.legend(title="Country/Region", bbox_to_anchor=(1.05, 1), loc="upper left")
|
| 39 |
ax.grid(True, linestyle=":", alpha=0.7)
|
| 40 |
ax.set_facecolor("#f5f5f5") # Light grey background
|
| 41 |
|
| 42 |
# Improve x-axis ticks
|
| 43 |
ax.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
|
| 44 |
+
plt.xticks(rotation=45, ha="right")
|
| 45 |
|
| 46 |
plt.tight_layout() # Adjust layout to prevent labels from overlapping
|
| 47 |
|
|
|
|
| 52 |
return fig, pd.DataFrame()
|
| 53 |
|
| 54 |
# Gradio Interface
|
| 55 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 56 |
+
gr.Markdown("# 📊 Top 5% Income Share Visualization")
|
| 57 |
+
gr.Markdown("This interactive application visualizes the share of income received by the richest 5% of the population across different countries.")
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
+
with gr.Column(scale=1):
|
| 61 |
entity_selector = gr.CheckboxGroup(
|
| 62 |
choices=entities,
|
| 63 |
+
label="Select Countries/Regions",
|
| 64 |
value=["Australia", "China", "Germany", "Japan", "United States"]
|
| 65 |
)
|
| 66 |
year_slider = gr.Slider(
|
|
|
|
| 68 |
maximum=latest_year,
|
| 69 |
value=[earliest_year, latest_year],
|
| 70 |
step=1,
|
| 71 |
+
label="Select Year Range"
|
| 72 |
)
|
| 73 |
+
show_raw_data_checkbox = gr.Checkbox(label="Show Raw Data Table", value=False)
|
| 74 |
+
submit_button = gr.Button("🚀 Update Visualization", variant="primary")
|
| 75 |
+
|
| 76 |
+
with gr.Column(scale=2):
|
| 77 |
+
output_plot = gr.Plot(label="Income Share Trend")
|
| 78 |
+
output_dataframe = gr.DataFrame(label="Filtered Data Table")
|
| 79 |
|
| 80 |
+
gr.Markdown("---")
|
| 81 |
+
gr.Markdown("**Source:** [Our World in Data](https://ourworldindata.org/grapher/top-5-income-share)")
|
|
|
|
| 82 |
|
| 83 |
submit_button.click(
|
| 84 |
fn=plot_data,
|