File size: 1,686 Bytes
68924be
1f3a826
 
4bd9b20
1f3a826
4bd9b20
1f3a826
 
 
 
 
4bd9b20
1f3a826
de34a83
4bd9b20
 
 
 
 
de34a83
4bd9b20
 
 
1f3a826
 
 
de34a83
 
 
 
 
 
 
 
 
1f3a826
4bd9b20
1f3a826
de34a83
 
 
 
 
 
1f3a826
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
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Sample data
data = {
    "Country": ["India", "China", "USA", "Indonesia", "Brazil"],
    "Population": [1400000000, 1410000000, 331000000, 273000000, 213000000]
}
df = pd.DataFrame(data)
df.set_index("Country", inplace=True)

# Heatmap function
def generate_heatmap(selected_countries):
    filtered_df = df.loc[selected_countries]
    normalized = filtered_df.copy()
    normalized["Population"] = normalized["Population"] / 1e6  # Convert to millions

    plt.figure(figsize=(6, 3))
    sns.heatmap(normalized.T, annot=True, fmt=".1f", cmap="YlOrRd", cbar_kws={"label": "Population (in millions)"})
    plt.title("🌍 Population Heatmap")
    plt.yticks(rotation=0)
    plt.tight_layout()
    return plt

# Pie chart function
def generate_pie_chart(selected_countries):
    filtered_df = df.loc[selected_countries]

    plt.figure(figsize=(5, 5))
    plt.pie(filtered_df["Population"], labels=filtered_df.index, 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 Visualization")
    selected = gr.CheckboxGroup(label="Select Countries", choices=df.index.tolist(), value=["India", "China", "USA"])

    with gr.Row():
        heatmap_output = gr.Plot(label="Heatmap")
        piechart_output = gr.Plot(label="Pie Chart")

    # Link input to both outputs
    selected.change(fn=generate_heatmap, inputs=selected, outputs=heatmap_output)
    selected.change(fn=generate_pie_chart, inputs=selected, outputs=piechart_output)

demo.launch()