Sanjayraju30 commited on
Commit
4bd9b20
·
verified ·
1 Parent(s): 501420b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -21
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 population data (you can use a CSV instead)
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 plot_chart(countries, chart_type):
13
- filtered_df = df[df["Country"].isin(countries)]
14
- plt.figure(figsize=(8, 5))
15
-
16
- if chart_type == "Bar":
17
- plt.bar(filtered_df["Country"], filtered_df["Population"], color="skyblue")
18
- plt.ylabel("Population")
19
- else: # Pie chart
20
- plt.pie(filtered_df["Population"], labels=filtered_df["Country"], autopct="%1.1f%%")
21
-
22
- plt.title("Population by Country")
23
  plt.tight_layout()
24
  return plt
25
 
26
- country_choices = df["Country"].tolist()
 
27
 
 
28
  with gr.Blocks() as demo:
29
- gr.Markdown("## 🌍 World Population Chart")
30
- with gr.Row():
31
- country_select = gr.CheckboxGroup(label="Select Countries", choices=country_choices, value=["India", "China"])
32
- chart_type = gr.Radio(label="Chart Type", choices=["Bar", "Pie"], value="Bar")
33
- output = gr.Plot()
34
 
35
- country_select.change(plot_chart, inputs=[country_select, chart_type], outputs=output)
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()