Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import matplotlib.pyplot as plt
|
|
|
4 |
|
5 |
-
# Sample
|
6 |
data = {
|
7 |
"Country": ["India", "China", "USA", "Indonesia", "Brazil"],
|
8 |
"Population": [1400000000, 1410000000, 331000000, 273000000, 213000000]
|
9 |
}
|
10 |
df = pd.DataFrame(data)
|
|
|
11 |
|
12 |
-
def
|
13 |
-
filtered_df = df[
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
plt.
|
23 |
plt.tight_layout()
|
24 |
return plt
|
25 |
|
26 |
-
|
|
|
27 |
|
|
|
28 |
with gr.Blocks() as demo:
|
29 |
-
gr.Markdown("##
|
30 |
-
|
31 |
-
|
32 |
-
chart_type = gr.Radio(label="Chart Type", choices=["Bar", "Pie"], value="Bar")
|
33 |
-
output = gr.Plot()
|
34 |
|
35 |
-
|
36 |
-
chart_type.change(plot_chart, inputs=[country_select, chart_type], outputs=output)
|
37 |
|
38 |
demo.launch()
|
39 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import matplotlib.pyplot as plt
|
4 |
+
import seaborn as sns
|
5 |
|
6 |
+
# Sample data
|
7 |
data = {
|
8 |
"Country": ["India", "China", "USA", "Indonesia", "Brazil"],
|
9 |
"Population": [1400000000, 1410000000, 331000000, 273000000, 213000000]
|
10 |
}
|
11 |
df = pd.DataFrame(data)
|
12 |
+
df.set_index("Country", inplace=True)
|
13 |
|
14 |
+
def generate_heatmap(selected_countries):
|
15 |
+
filtered_df = df.loc[selected_countries]
|
16 |
+
|
17 |
+
# Normalize for better visualization
|
18 |
+
normalized = filtered_df.copy()
|
19 |
+
normalized["Population"] = normalized["Population"] / 1e6 # Convert to millions
|
20 |
+
|
21 |
+
plt.figure(figsize=(8, 4))
|
22 |
+
sns.heatmap(normalized.T, annot=True, fmt=".1f", cmap="YlOrRd", cbar_kws={"label": "Population (in millions)"})
|
23 |
+
plt.title("🌍 Population Heatmap")
|
24 |
+
plt.yticks(rotation=0)
|
25 |
plt.tight_layout()
|
26 |
return plt
|
27 |
|
28 |
+
# Country options
|
29 |
+
country_choices = df.index.tolist()
|
30 |
|
31 |
+
# Gradio App
|
32 |
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("## 🌡️ World Population Heatmap")
|
34 |
+
selected = gr.CheckboxGroup(label="Select Countries", choices=country_choices, value=["India", "China", "USA"])
|
35 |
+
out = gr.Plot()
|
|
|
|
|
36 |
|
37 |
+
selected.change(fn=generate_heatmap, inputs=selected, outputs=out)
|
|
|
38 |
|
39 |
demo.launch()
|
|