Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,37 +2,81 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
| 5 |
|
| 6 |
# Load the data
|
| 7 |
-
df = pd.read_csv(
|
| 8 |
|
| 9 |
-
# Get unique entities for
|
| 10 |
-
entities = df[
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def plot_data(selected_entities):
|
| 13 |
if not selected_entities:
|
| 14 |
-
return plt.figure()
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
for entity in selected_entities:
|
| 18 |
-
|
| 19 |
-
ax.plot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return fig
|
| 27 |
|
| 28 |
# Gradio Interface
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
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 |
|
| 10 |
+
# Get unique entities and years for dropdowns and sliders
|
| 11 |
+
entities = sorted(df["Entity"].unique().tolist())
|
| 12 |
+
earliest_year = int(df["Year"].min())
|
| 13 |
+
latest_year = int(df["Year"].max())
|
| 14 |
|
| 15 |
+
def plot_data(selected_entities, year_range, show_raw_data):
|
| 16 |
if not selected_entities:
|
| 17 |
+
return plt.figure(), pd.DataFrame()
|
| 18 |
|
| 19 |
+
min_year, max_year = year_range
|
| 20 |
+
|
| 21 |
+
filtered_df = df[df["Entity"].isin(selected_entities)].copy()
|
| 22 |
+
filtered_df = filtered_df[(filtered_df["Year"] >= min_year) & (filtered_df["Year"] <= max_year)]
|
| 23 |
+
|
| 24 |
+
# Create the plot
|
| 25 |
+
fig, ax = plt.subplots(figsize=(12, 7))
|
| 26 |
for entity in selected_entities:
|
| 27 |
+
entity_data = filtered_df[filtered_df["Entity"] == entity]
|
| 28 |
+
ax.plot(entity_data["Year"], entity_data["Percent"], label=entity, marker=".", markersize=8)
|
| 29 |
+
|
| 30 |
+
ax.set_xlabel("年份", fontsize=12)
|
| 31 |
+
ax.set_ylabel("收入份额百分比", fontsize=12)
|
| 32 |
+
ax.set_title("Top 5% 收入份额", fontsize=16, fontweight=\"bold\")
|
| 33 |
+
ax.legend(title="国家/地区", bbox_to_anchor=(1.05, 1), loc=\"upper left\")
|
| 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=\"right\")
|
| 40 |
+
|
| 41 |
+
plt.tight_layout() # Adjust layout to prevent labels from overlapping
|
| 42 |
|
| 43 |
+
# Return raw data if requested
|
| 44 |
+
if show_raw_data:
|
| 45 |
+
return fig, filtered_df
|
| 46 |
+
else:
|
| 47 |
+
return fig, pd.DataFrame()
|
|
|
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("# Top 5% 收入份额可视化")
|
| 52 |
+
gr.Markdown("此应用展示了不同国家/地区人口中收入最高的 5% 人群的收入份额。")
|
| 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(
|
| 62 |
+
minimum=earliest_year,
|
| 63 |
+
maximum=latest_year,
|
| 64 |
+
value=[earliest_year, latest_year],
|
| 65 |
+
step=1,
|
| 66 |
+
label="选择年份范围"
|
| 67 |
+
)
|
| 68 |
+
show_raw_data_checkbox = gr.Checkbox(label="显示原始数据", value=False)
|
| 69 |
+
submit_button = gr.Button("生成图表")
|
| 70 |
+
|
| 71 |
+
with gr.Column():
|
| 72 |
+
output_plot = gr.Plot(label="收入份额趋势图")
|
| 73 |
+
output_dataframe = gr.DataFrame(label="原始数据")
|
| 74 |
+
|
| 75 |
+
submit_button.click(
|
| 76 |
+
fn=plot_data,
|
| 77 |
+
inputs=[entity_selector, year_slider, show_raw_data_checkbox],
|
| 78 |
+
outputs=[output_plot, output_dataframe]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|