Skip to content

Commit 800db6b

Browse files
feat(plotly): implement choropleth-basic (#3112)
## Implementation: `choropleth-basic` - plotly Implements the **plotly** version of `choropleth-basic`. **File:** `plots/choropleth-basic/implementations/plotly.py` **Parent Issue:** #3069 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20620322406)* --------- 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 341c5d2 commit 800db6b

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
""" pyplots.ai
2+
choropleth-basic: Choropleth Map with Regional Coloring
3+
Library: plotly 6.5.0 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-31
5+
"""
6+
7+
import numpy as np
8+
import pandas as pd
9+
import plotly.express as px
10+
11+
12+
# Data - GDP per capita for European countries (realistic economic indicator)
13+
np.random.seed(42)
14+
15+
# European country ISO codes and approximate GDP per capita (USD)
16+
countries = [
17+
"DEU",
18+
"FRA",
19+
"GBR",
20+
"ITA",
21+
"ESP",
22+
"POL",
23+
"NLD",
24+
"BEL",
25+
"SWE",
26+
"AUT",
27+
"CHE",
28+
"NOR",
29+
"DNK",
30+
"FIN",
31+
"IRL",
32+
"PRT",
33+
"GRC",
34+
"CZE",
35+
"ROU",
36+
"HUN",
37+
"SVK",
38+
"BGR",
39+
"HRV",
40+
"SVN",
41+
"EST",
42+
"LVA",
43+
"LTU",
44+
"LUX",
45+
"ISL",
46+
"SRB",
47+
]
48+
49+
# Realistic GDP per capita values (in thousands USD) - varied distribution
50+
gdp_values = [
51+
52.0,
52+
44.0,
53+
46.0,
54+
35.0,
55+
30.0,
56+
18.0,
57+
58.0,
58+
51.0,
59+
56.0,
60+
53.0,
61+
92.0,
62+
89.0,
63+
68.0,
64+
54.0,
65+
103.0,
66+
24.0,
67+
20.0,
68+
27.0,
69+
15.0,
70+
18.0,
71+
21.0,
72+
12.0,
73+
17.0,
74+
29.0,
75+
28.0,
76+
22.0,
77+
24.0,
78+
125.0,
79+
75.0,
80+
9.0,
81+
]
82+
83+
df = pd.DataFrame({"country": countries, "gdp_per_capita": gdp_values})
84+
85+
# Create choropleth map
86+
fig = px.choropleth(
87+
df,
88+
locations="country",
89+
locationmode="ISO-3",
90+
color="gdp_per_capita",
91+
color_continuous_scale="Viridis",
92+
scope="europe",
93+
labels={"gdp_per_capita": "GDP per Capita (k USD)"},
94+
)
95+
96+
# Update layout for large canvas
97+
fig.update_layout(
98+
title={
99+
"text": "GDP per Capita in Europe · choropleth-basic · plotly · pyplots.ai",
100+
"font": {"size": 28},
101+
"x": 0.5,
102+
"xanchor": "center",
103+
},
104+
geo={
105+
"showframe": False,
106+
"showcoastlines": True,
107+
"coastlinecolor": "gray",
108+
"coastlinewidth": 1,
109+
"showland": True,
110+
"landcolor": "lightgray",
111+
"showocean": True,
112+
"oceancolor": "aliceblue",
113+
"showlakes": True,
114+
"lakecolor": "aliceblue",
115+
"projection_type": "natural earth",
116+
"bgcolor": "white",
117+
},
118+
coloraxis_colorbar={
119+
"title": {"text": "GDP per Capita<br>(k USD)", "font": {"size": 20}},
120+
"tickfont": {"size": 16},
121+
"len": 0.7,
122+
"thickness": 25,
123+
"x": 0.95,
124+
},
125+
template="plotly_white",
126+
margin={"l": 20, "r": 80, "t": 80, "b": 20},
127+
)
128+
129+
# Update traces for better visibility
130+
fig.update_traces(marker_line_color="darkgray", marker_line_width=0.5)
131+
132+
# Save as PNG (4800x2700 px)
133+
fig.write_image("plot.png", width=1600, height=900, scale=3)
134+
135+
# Save as HTML for interactivity
136+
fig.write_html("plot.html", include_plotlyjs=True, full_html=True)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library: plotly
2+
specification_id: choropleth-basic
3+
created: '2025-12-31T13:56:19Z'
4+
updated: '2025-12-31T14:08:32Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20620322406
7+
issue: 3069
8+
python_version: 3.13.11
9+
library_version: 6.5.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/plotly/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/plotly/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/choropleth-basic/plotly/plot.html
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent choice of realistic European GDP data with proper ISO-3 country codes
17+
- Clean Viridis colorblind-safe color palette with appropriate value range
18+
- Well-formatted title following the required naming convention
19+
- Good use of Plotly geographic features (scope, projection, coastlines, ocean coloring)
20+
- Generates both PNG and interactive HTML output
21+
- Missing data countries shown gracefully in gray
22+
weaknesses:
23+
- Title format includes extra prefix before the spec-id pattern
24+
- Country border lines could be slightly thicker for better boundary visibility
25+
- Could add hover templates to enhance interactive HTML with formatted GDP values

0 commit comments

Comments
 (0)