Skip to content

Commit 188b0ba

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

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
""" pyplots.ai
2+
choropleth-basic: Choropleth Map with Regional Coloring
3+
Library: letsplot 4.8.2 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-31
5+
"""
6+
7+
import pandas as pd
8+
from lets_plot import * # noqa: F403
9+
from lets_plot.export import ggsave as export_ggsave
10+
from lets_plot.geo_data import geocode_countries
11+
12+
13+
LetsPlot.setup_html() # noqa: F405
14+
15+
# Data: GDP per capita by European countries (in thousands USD)
16+
data = {
17+
"country": [
18+
"Germany",
19+
"France",
20+
"Italy",
21+
"Spain",
22+
"Poland",
23+
"Netherlands",
24+
"Belgium",
25+
"Sweden",
26+
"Austria",
27+
"Switzerland",
28+
"Norway",
29+
"Denmark",
30+
"Finland",
31+
"Ireland",
32+
"Portugal",
33+
"Czech Republic",
34+
"Greece",
35+
"Hungary",
36+
"Romania",
37+
"Bulgaria",
38+
"Slovakia",
39+
"Croatia",
40+
"Slovenia",
41+
"Lithuania",
42+
"Latvia",
43+
"Estonia",
44+
"Luxembourg",
45+
],
46+
"gdp_per_capita": [
47+
48.7,
48+
42.3,
49+
34.5,
50+
30.1,
51+
17.8,
52+
57.0,
53+
51.2,
54+
55.7,
55+
53.3,
56+
92.4,
57+
89.2,
58+
67.8,
59+
53.2,
60+
100.2,
61+
24.5,
62+
27.0,
63+
20.2,
64+
18.8,
65+
15.1,
66+
13.9,
67+
21.3,
68+
18.5,
69+
28.4,
70+
24.0,
71+
21.8,
72+
28.3,
73+
126.4,
74+
],
75+
}
76+
df = pd.DataFrame(data)
77+
78+
# Get country boundaries
79+
countries = geocode_countries(df["country"].tolist()).get_boundaries()
80+
df_geo = countries.merge(df, left_on="found name", right_on="country", how="left")
81+
82+
# Create choropleth map with European focus
83+
plot = (
84+
ggplot() # noqa: F405
85+
+ geom_map( # noqa: F405
86+
aes(fill="gdp_per_capita"), # noqa: F405
87+
data=df,
88+
map=df_geo,
89+
map_join=["country", "found name"],
90+
color="#404040",
91+
size=0.6,
92+
alpha=0.95,
93+
)
94+
+ scale_fill_gradient( # noqa: F405
95+
low="#FFD43B", high="#306998", name="GDP per Capita\n(thousands USD)", na_value="#E0E0E0"
96+
)
97+
+ labs(title="European GDP per Capita · choropleth-basic · letsplot · pyplots.ai") # noqa: F405
98+
+ coord_cartesian(xlim=[-12, 32], ylim=[35, 71]) # noqa: F405
99+
+ ggsize(1600, 900) # noqa: F405
100+
+ theme_minimal() # noqa: F405
101+
+ theme( # noqa: F405
102+
plot_title=element_text(size=26, face="bold"), # noqa: F405
103+
legend_title=element_text(size=18), # noqa: F405
104+
legend_text=element_text(size=16), # noqa: F405
105+
legend_position=[0.88, 0.35],
106+
axis_title=element_blank(), # noqa: F405
107+
axis_text=element_blank(), # noqa: F405
108+
axis_ticks=element_blank(), # noqa: F405
109+
panel_grid=element_blank(), # noqa: F405
110+
plot_background=element_rect(fill="white"), # noqa: F405
111+
)
112+
)
113+
114+
# Save as PNG (scale 3x for 4800 × 2700 px)
115+
export_ggsave(plot, "plot.png", path=".", scale=3)
116+
117+
# Save interactive HTML
118+
export_ggsave(plot, "plot.html", path=".")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
library: letsplot
2+
specification_id: choropleth-basic
3+
created: '2025-12-31T13:57:47Z'
4+
updated: '2025-12-31T14:09:44Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20620327888
7+
issue: 3069
8+
python_version: 3.13.11
9+
library_version: 4.8.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/letsplot/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/letsplot/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/letsplot/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent use of lets-plot geocode_countries() for automatic boundary retrieval
17+
- Clean professional appearance with appropriate coordinate limits focusing on Europe
18+
- Good colorblind-accessible yellow-blue color scheme
19+
- Proper missing data handling with na_value parameter
20+
- Well-structured theme with appropriate removal of axes/grid for map visualization
21+
- Generates both PNG and HTML outputs for interactive capability
22+
weaknesses:
23+
- Legend could be larger and more prominent for better readability at full size
24+
- Some European countries missing from data (UK, Iceland, Southern Europe) - more
25+
coverage recommended
26+
- Extreme outlier (Luxembourg 126.4k) compresses color scale making mid-range countries
27+
harder to distinguish

0 commit comments

Comments
 (0)