Skip to content

Commit 1d0eab2

Browse files
feat(altair): implement line-styled (#2621)
## Implementation: `line-styled` - altair Implements the **altair** version of `line-styled`. **File:** `plots/line-styled/implementations/altair.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20594557223)* --------- 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 a7a4d56 commit 1d0eab2

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
""" pyplots.ai
2+
line-styled: Styled Line Plot
3+
Library: altair 6.0.0 | Python 3.13.11
4+
Quality: 93/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 - Monthly temperature readings from different weather stations
13+
np.random.seed(42)
14+
months = np.arange(1, 13)
15+
month_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
16+
17+
# Temperature data for 4 weather stations with seasonal patterns
18+
base_temp = np.array([5, 7, 12, 16, 21, 25, 28, 27, 23, 17, 11, 6])
19+
station_a = base_temp + np.random.randn(12) * 1.5
20+
station_b = base_temp - 3 + np.random.randn(12) * 1.5
21+
station_c = base_temp + 5 + np.random.randn(12) * 1.5
22+
station_d = base_temp - 6 + np.random.randn(12) * 1.5
23+
24+
# Create long-form DataFrame for Altair
25+
df = pd.DataFrame(
26+
{
27+
"Month": month_names * 4,
28+
"MonthNum": list(months) * 4,
29+
"Temperature": np.concatenate([station_a, station_b, station_c, station_d]),
30+
"Station": (["Coastal"] * 12 + ["Mountain"] * 12 + ["Valley"] * 12 + ["Highland"] * 12),
31+
}
32+
)
33+
34+
# Define line styles (strokeDash) for each station
35+
line_styles = {
36+
"Coastal": [0], # solid
37+
"Mountain": [8, 4], # dashed
38+
"Valley": [2, 2], # dotted
39+
"Highland": [8, 4, 2, 4], # dash-dot
40+
}
41+
42+
# Colors - using Python palette and accessible colors
43+
station_colors = {
44+
"Coastal": "#306998", # Python Blue
45+
"Mountain": "#FFD43B", # Python Yellow
46+
"Valley": "#48A9A6", # Teal
47+
"Highland": "#E76F51", # Coral
48+
}
49+
50+
# Create the chart
51+
chart = (
52+
alt.Chart(df)
53+
.mark_line(
54+
strokeWidth=4 # Thicker lines for visibility
55+
)
56+
.encode(
57+
x=alt.X(
58+
"MonthNum:O",
59+
title="Month",
60+
axis=alt.Axis(
61+
labelFontSize=18,
62+
titleFontSize=22,
63+
values=list(months),
64+
labelExpr="['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][datum.value - 1]",
65+
),
66+
),
67+
y=alt.Y(
68+
"Temperature:Q",
69+
title="Temperature (\u00b0C)",
70+
scale=alt.Scale(domain=[-5, 40]),
71+
axis=alt.Axis(labelFontSize=18, titleFontSize=22),
72+
),
73+
color=alt.Color(
74+
"Station:N",
75+
scale=alt.Scale(domain=list(station_colors.keys()), range=list(station_colors.values())),
76+
legend=alt.Legend(
77+
title="Weather Station", titleFontSize=20, labelFontSize=18, symbolStrokeWidth=4, symbolSize=300
78+
),
79+
),
80+
strokeDash=alt.StrokeDash(
81+
"Station:N", scale=alt.Scale(domain=list(line_styles.keys()), range=list(line_styles.values())), legend=None
82+
), # Combine with color legend
83+
)
84+
.properties(
85+
width=1600,
86+
height=900,
87+
title=alt.Title(text="line-styled \u00b7 altair \u00b7 pyplots.ai", fontSize=28, anchor="middle"),
88+
)
89+
.configure_axis(grid=True, gridOpacity=0.3, gridDash=[2, 2])
90+
.configure_view(strokeWidth=0)
91+
)
92+
93+
# Save as PNG (scale_factor=3 gives 4800x2700)
94+
chart.save("plot.png", scale_factor=3.0)
95+
96+
# Save as HTML for interactivity
97+
chart.save("plot.html")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library: altair
2+
specification_id: line-styled
3+
created: '2025-12-30T10:38:24Z'
4+
updated: '2025-12-30T10:46:24Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20594557223
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/line-styled/altair/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/line-styled/altair/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/line-styled/altair/plot.html
13+
quality_score: 93
14+
review:
15+
strengths:
16+
- Excellent use of all four standard line styles (solid, dashed, dotted, dash-dot)
17+
clearly distinguishable
18+
- Beautiful color palette with Python Blue and accessible complementary colors
19+
- Professional typography with well-sized fonts for 4800x2700 output
20+
- Realistic weather station temperature scenario with seasonal patterns
21+
- Clean, KISS-compliant code structure following Altair best practices
22+
weaknesses:
23+
- Legend shows only color swatches, not the actual line styles (Altair limitation
24+
- strokeDash legend combined with color shows color only)
25+
- No interactive features (tooltips, hover) which are Altair distinctive strength

0 commit comments

Comments
 (0)