Sanjayraju30 commited on
Commit
1786e22
Β·
verified Β·
1 Parent(s): 0ae693a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -1,50 +1,54 @@
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
- # 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()
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import plotly.express as px
4
  import matplotlib.pyplot as plt
 
5
 
6
+ # Sample data with coordinates (approximate)
7
  data = {
8
  "Country": ["India", "China", "USA", "Indonesia", "Brazil"],
9
+ "Population": [1400000000, 1410000000, 331000000, 273000000, 213000000],
10
+ "Latitude": [20.5937, 35.8617, 37.0902, -0.7893, -14.2350],
11
+ "Longitude": [78.9629, 104.1954, -95.7129, 113.9213, -51.9253]
12
  }
13
  df = pd.DataFrame(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Geo heatmap function
16
+ def generate_geo_heatmap(selected_countries):
17
+ filtered_df = df[df["Country"].isin(selected_countries)]
18
+
19
+ fig = px.density_mapbox(
20
+ filtered_df,
21
+ lat="Latitude",
22
+ lon="Longitude",
23
+ z="Population",
24
+ hover_name="Country",
25
+ radius=30,
26
+ center=dict(lat=20, lon=0),
27
+ zoom=1,
28
+ mapbox_style="carto-positron"
29
+ )
30
+ fig.update_layout(title="🌍 World Population Heatmap (Globe Map)", height=500)
31
+ return fig
32
+
33
+ # Pie chart function (same as before)
34
  def generate_pie_chart(selected_countries):
35
+ filtered_df = df[df["Country"].isin(selected_countries)]
 
36
  plt.figure(figsize=(5, 5))
37
+ plt.pie(filtered_df["Population"], labels=filtered_df["Country"], autopct="%1.1f%%", startangle=90)
38
  plt.title("🧩 Population Distribution")
39
  plt.tight_layout()
40
  return plt
41
 
42
  # Gradio App
43
  with gr.Blocks() as demo:
44
+ gr.Markdown("## 🌍 World Population Heatmap and Pie Chart")
45
+ selected = gr.CheckboxGroup(label="Select Countries", choices=df["Country"].tolist(), value=["India", "China", "USA"])
46
 
47
  with gr.Row():
48
+ heatmap_output = gr.Plot(label="World Heatmap (Map View)")
49
  piechart_output = gr.Plot(label="Pie Chart")
50
 
51
+ selected.change(fn=generate_geo_heatmap, inputs=selected, outputs=heatmap_output)
 
52
  selected.change(fn=generate_pie_chart, inputs=selected, outputs=piechart_output)
53
 
54
  demo.launch()