Sanjayraju30 commited on
Commit
de34a83
Β·
verified Β·
1 Parent(s): c8e0041

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -11,29 +11,40 @@ data = {
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()
 
11
  df = pd.DataFrame(data)
12
  df.set_index("Country", inplace=True)
13
 
14
+ # Heatmap function
15
  def generate_heatmap(selected_countries):
16
  filtered_df = df.loc[selected_countries]
 
 
17
  normalized = filtered_df.copy()
18
  normalized["Population"] = normalized["Population"] / 1e6 # Convert to millions
19
 
20
+ plt.figure(figsize=(6, 3))
21
  sns.heatmap(normalized.T, annot=True, fmt=".1f", cmap="YlOrRd", cbar_kws={"label": "Population (in millions)"})
22
  plt.title("🌍 Population Heatmap")
23
  plt.yticks(rotation=0)
24
  plt.tight_layout()
25
  return plt
26
 
27
+ # Pie chart function
28
+ def generate_pie_chart(selected_countries):
29
+ filtered_df = df.loc[selected_countries]
30
+
31
+ plt.figure(figsize=(5, 5))
32
+ plt.pie(filtered_df["Population"], labels=filtered_df.index, autopct="%1.1f%%", startangle=90)
33
+ plt.title("🧩 Population Distribution")
34
+ plt.tight_layout()
35
+ return plt
36
 
37
  # Gradio App
38
  with gr.Blocks() as demo:
39
+ gr.Markdown("## 🌍 World Population Visualization")
40
+ selected = gr.CheckboxGroup(label="Select Countries", choices=df.index.tolist(), value=["India", "China", "USA"])
41
+
42
+ with gr.Row():
43
+ heatmap_output = gr.Plot(label="Heatmap")
44
+ piechart_output = gr.Plot(label="Pie Chart")
45
 
46
+ # Link input to both outputs
47
+ selected.change(fn=generate_heatmap, inputs=selected, outputs=heatmap_output)
48
+ selected.change(fn=generate_pie_chart, inputs=selected, outputs=piechart_output)
49
 
50
  demo.launch()