Skip to content

Commit 981d533

Browse files
feat(altair): implement polar-bar (#2749)
## Implementation: `polar-bar` - altair Implements the **altair** version of `polar-bar`. **File:** `plots/polar-bar/implementations/altair.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20601057527)* --------- 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 9fc07da commit 981d533

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
""" pyplots.ai
2+
polar-bar: Polar Bar Chart (Wind Rose)
3+
Library: altair 6.0.0 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-30
5+
"""
6+
7+
import altair as alt
8+
import numpy as np
9+
import pandas as pd
10+
11+
12+
# Data - Wind direction frequency data
13+
np.random.seed(42)
14+
directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
15+
angles = [0, 45, 90, 135, 180, 225, 270, 315] # degrees from north
16+
17+
# Simulate realistic wind frequency with prevailing westerlies
18+
base_freq = [12, 8, 10, 6, 9, 18, 22, 16] # Higher for W, SW, NW
19+
frequencies = [f + np.random.randint(-2, 3) for f in base_freq]
20+
21+
# Create DataFrame with angle in radians for Altair
22+
df = pd.DataFrame({"direction": directions, "angle": angles, "frequency": frequencies})
23+
24+
# Calculate start and end angles for each bar (in radians)
25+
# Altair uses radians, and we need to center each bar on its direction
26+
bar_width = 40 # degrees
27+
df["theta_start"] = np.radians(df["angle"] - bar_width / 2)
28+
df["theta_end"] = np.radians(df["angle"] + bar_width / 2)
29+
30+
# Create the polar bar chart using mark_arc
31+
base = alt.Chart(df)
32+
33+
# Bars radiating from center using arc marks
34+
bars = base.mark_arc(stroke="white", strokeWidth=2).encode(
35+
theta=alt.Theta("theta_start:Q", scale=alt.Scale(domain=[0, 2 * np.pi])),
36+
theta2="theta_end:Q",
37+
radius=alt.Radius("frequency:Q", scale=alt.Scale(type="linear", zero=True, rangeMin=0)),
38+
color=alt.Color(
39+
"frequency:Q",
40+
scale=alt.Scale(scheme="blues"),
41+
legend=alt.Legend(title="Frequency (days)", titleFontSize=18, labelFontSize=16, orient="right"),
42+
),
43+
tooltip=[alt.Tooltip("direction:N", title="Direction"), alt.Tooltip("frequency:Q", title="Frequency (days)")],
44+
)
45+
46+
# Add direction labels around the perimeter
47+
max_freq = max(frequencies) * 1.25
48+
label_df = pd.DataFrame({"direction": directions, "angle_rad": np.radians(angles), "radius": [max_freq] * 8})
49+
50+
# Calculate x, y positions for labels
51+
label_df["x"] = label_df["radius"] * np.sin(label_df["angle_rad"])
52+
label_df["y"] = label_df["radius"] * np.cos(label_df["angle_rad"])
53+
54+
labels = (
55+
alt.Chart(label_df)
56+
.mark_text(fontSize=20, fontWeight="bold", color="#306998")
57+
.encode(x=alt.X("x:Q", axis=None), y=alt.Y("y:Q", axis=None), text="direction:N")
58+
)
59+
60+
# Combine bars and labels
61+
chart = (
62+
alt.layer(bars, labels)
63+
.properties(
64+
width=900,
65+
height=900,
66+
title=alt.Title(text="polar-bar · altair · pyplots.ai", fontSize=28, anchor="middle", color="#333333"),
67+
)
68+
.configure_view(strokeWidth=0)
69+
.interactive()
70+
)
71+
72+
# Save outputs
73+
chart.save("plot.png", scale_factor=4.5)
74+
chart.save("plot.html")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library: altair
2+
specification_id: polar-bar
3+
created: '2025-12-30T16:29:55Z'
4+
updated: '2025-12-30T16:46:59Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20601057527
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 6.0.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/polar-bar/altair/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/polar-bar/altair/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/polar-bar/altair/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent use of mark_arc for creating polar bar chart wedges with proper theta/theta2
17+
angle calculations
18+
- Clean direction label positioning using calculated x/y coordinates from polar
19+
coordinates
20+
- Effective color encoding with sequential blues scheme that enhances data interpretation
21+
- Interactive tooltips provide additional data exploration capability
22+
- Realistic wind frequency data with prevailing westerlies pattern demonstrates
23+
domain knowledge
24+
weaknesses:
25+
- Legend title shows Frequency (days) but the scale numbers do not include units
26+
in tick labels
27+
- Scale factor 4.5 produces approximately 4050x4050 pixels which does not match
28+
the standard 3600x3600 or 4800x2700 target sizes
29+
- Could demonstrate stacked bars for wind speed ranges as mentioned in spec notes

0 commit comments

Comments
 (0)