Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
# Sample data | |
data = { | |
"Country": ["India", "China", "USA", "Indonesia", "Brazil"], | |
"Population": [1400000000, 1410000000, 331000000, 273000000, 213000000] | |
} | |
df = pd.DataFrame(data) | |
df.set_index("Country", inplace=True) | |
# Heatmap function | |
def generate_heatmap(selected_countries): | |
filtered_df = df.loc[selected_countries] | |
normalized = filtered_df.copy() | |
normalized["Population"] = normalized["Population"] / 1e6 # Convert to millions | |
plt.figure(figsize=(6, 3)) | |
sns.heatmap(normalized.T, annot=True, fmt=".1f", cmap="YlOrRd", cbar_kws={"label": "Population (in millions)"}) | |
plt.title("π Population Heatmap") | |
plt.yticks(rotation=0) | |
plt.tight_layout() | |
return plt | |
# Pie chart function | |
def generate_pie_chart(selected_countries): | |
filtered_df = df.loc[selected_countries] | |
plt.figure(figsize=(5, 5)) | |
plt.pie(filtered_df["Population"], labels=filtered_df.index, autopct="%1.1f%%", startangle=90) | |
plt.title("π§© Population Distribution") | |
plt.tight_layout() | |
return plt | |
# Gradio App | |
with gr.Blocks() as demo: | |
gr.Markdown("## π World Population Visualization") | |
selected = gr.CheckboxGroup(label="Select Countries", choices=df.index.tolist(), value=["India", "China", "USA"]) | |
with gr.Row(): | |
heatmap_output = gr.Plot(label="Heatmap") | |
piechart_output = gr.Plot(label="Pie Chart") | |
# Link input to both outputs | |
selected.change(fn=generate_heatmap, inputs=selected, outputs=heatmap_output) | |
selected.change(fn=generate_pie_chart, inputs=selected, outputs=piechart_output) | |
demo.launch() | |