Skip to content

Commit e1e2cab

Browse files
feat(pygal): implement choropleth-basic (#3116)
## Implementation: `choropleth-basic` - pygal Implements the **pygal** version of `choropleth-basic`. **File:** `plots/choropleth-basic/implementations/pygal.py` **Parent Issue:** #3069 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20620327898)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 188b0ba commit e1e2cab

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
""" pyplots.ai
2+
choropleth-basic: Choropleth Map with Regional Coloring
3+
Library: pygal 3.1.0 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-31
5+
"""
6+
7+
import numpy as np
8+
from pygal.style import Style
9+
from pygal_maps_world.maps import World
10+
11+
12+
# Data: GDP per capita (synthetic but realistic ranges)
13+
np.random.seed(42)
14+
15+
# Select a diverse set of countries from different regions
16+
country_codes = [
17+
# Americas
18+
"us",
19+
"ca",
20+
"mx",
21+
"br",
22+
"ar",
23+
"cl",
24+
"co",
25+
"pe",
26+
# Europe
27+
"de",
28+
"fr",
29+
"gb",
30+
"it",
31+
"es",
32+
"nl",
33+
"se",
34+
"no",
35+
"pl",
36+
"pt",
37+
# Asia
38+
"cn",
39+
"jp",
40+
"in",
41+
"kr",
42+
"id",
43+
"th",
44+
"vn",
45+
"my",
46+
# Africa
47+
"za",
48+
"eg",
49+
"ng",
50+
"ke",
51+
"ma",
52+
"gh",
53+
# Oceania
54+
"au",
55+
"nz",
56+
]
57+
58+
# Generate realistic GDP per capita values (in thousands USD)
59+
high_income = ["us", "ca", "de", "fr", "gb", "nl", "se", "no", "jp", "kr", "au", "nz"]
60+
upper_middle = ["mx", "br", "ar", "cl", "cn", "my", "za", "pl", "pt", "it", "es"]
61+
62+
gdp_data = {}
63+
for code in country_codes:
64+
if code in high_income:
65+
gdp_data[code] = np.random.uniform(40, 85)
66+
elif code in upper_middle:
67+
gdp_data[code] = np.random.uniform(10, 40)
68+
else:
69+
gdp_data[code] = np.random.uniform(1, 15)
70+
71+
# Bin data into ranges for legend clarity
72+
bins = [
73+
("GDP < $10k", {k: v for k, v in gdp_data.items() if v < 10}),
74+
("GDP $10k-$25k", {k: v for k, v in gdp_data.items() if 10 <= v < 25}),
75+
("GDP $25k-$50k", {k: v for k, v in gdp_data.items() if 25 <= v < 50}),
76+
("GDP > $50k", {k: v for k, v in gdp_data.items() if v >= 50}),
77+
]
78+
79+
# Sequential blue palette for choropleth (light to dark)
80+
colors = ["#a6cee3", "#6baed6", "#3182bd", "#08519c"]
81+
82+
# Custom style for large canvas
83+
custom_style = Style(
84+
background="white",
85+
plot_background="white",
86+
foreground="#333333",
87+
foreground_strong="#111111",
88+
foreground_subtle="#666666",
89+
colors=tuple(colors),
90+
title_font_size=80,
91+
label_font_size=48,
92+
legend_font_size=48,
93+
major_label_font_size=40,
94+
value_font_size=40,
95+
tooltip_font_size=36,
96+
no_data_font_size=36,
97+
)
98+
99+
# Create world map
100+
worldmap = World(
101+
style=custom_style,
102+
width=4800,
103+
height=2700,
104+
title="choropleth-basic \u00b7 pygal \u00b7 pyplots.ai",
105+
show_legend=True,
106+
legend_at_bottom=True,
107+
legend_at_bottom_columns=4,
108+
legend_box_size=40,
109+
)
110+
111+
# Add each bin as a separate series
112+
for label, data in bins:
113+
worldmap.add(label, data)
114+
115+
# Save outputs
116+
worldmap.render_to_file("plot.html")
117+
worldmap.render_to_png("plot.png")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library: pygal
2+
specification_id: choropleth-basic
3+
created: '2025-12-31T13:57:54Z'
4+
updated: '2025-12-31T14:09:13Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20620327898
7+
issue: 3069
8+
python_version: 3.13.11
9+
library_version: 3.1.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/pygal/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/pygal/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/pygal/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent use of pygal_maps_world extension for creating a world choropleth map
17+
- Sequential blue color palette is visually appealing and colorblind-accessible
18+
- Good binning approach that creates clear visual distinctions between income levels
19+
- Comprehensive geographic coverage spanning all major continents
20+
- Custom Style properly scales fonts for the 4800x2700 canvas size
21+
- Legend placement at bottom with 4 columns efficiently organizes the GDP ranges
22+
weaknesses:
23+
- Missing data countries shown as white rather than gray as suggested in spec
24+
- Could leverage pygal's built-in tooltip/hover interactivity more prominently

0 commit comments

Comments
 (0)