Spaces:
Sleeping
Sleeping
File size: 1,873 Bytes
68924be 1f3a826 1786e22 1f3a826 1786e22 1f3a826 1786e22 1f3a826 1786e22 de34a83 1786e22 de34a83 1786e22 de34a83 1f3a826 4bd9b20 1f3a826 1786e22 de34a83 1786e22 de34a83 1f3a826 1786e22 de34a83 1f3a826 |
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 |
import gradio as gr
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
# Sample data with coordinates (approximate)
data = {
"Country": ["India", "China", "USA", "Indonesia", "Brazil"],
"Population": [1400000000, 1410000000, 331000000, 273000000, 213000000],
"Latitude": [20.5937, 35.8617, 37.0902, -0.7893, -14.2350],
"Longitude": [78.9629, 104.1954, -95.7129, 113.9213, -51.9253]
}
df = pd.DataFrame(data)
# Geo heatmap function
def generate_geo_heatmap(selected_countries):
filtered_df = df[df["Country"].isin(selected_countries)]
fig = px.density_mapbox(
filtered_df,
lat="Latitude",
lon="Longitude",
z="Population",
hover_name="Country",
radius=30,
center=dict(lat=20, lon=0),
zoom=1,
mapbox_style="carto-positron"
)
fig.update_layout(title="π World Population Heatmap (Globe Map)", height=500)
return fig
# Pie chart function (same as before)
def generate_pie_chart(selected_countries):
filtered_df = df[df["Country"].isin(selected_countries)]
plt.figure(figsize=(5, 5))
plt.pie(filtered_df["Population"], labels=filtered_df["Country"], 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 Heatmap and Pie Chart")
selected = gr.CheckboxGroup(label="Select Countries", choices=df["Country"].tolist(), value=["India", "China", "USA"])
with gr.Row():
heatmap_output = gr.Plot(label="World Heatmap (Map View)")
piechart_output = gr.Plot(label="Pie Chart")
selected.change(fn=generate_geo_heatmap, inputs=selected, outputs=heatmap_output)
selected.change(fn=generate_pie_chart, inputs=selected, outputs=piechart_output)
demo.launch()
|