Skip to content

Commit 3e34794

Browse files
feat(altair): implement slider-control-basic (#3101)
## Implementation: `slider-control-basic` - altair Implements the **altair** version of `slider-control-basic`. **File:** `plots/slider-control-basic/implementations/altair.py` **Parent Issue:** #3071 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20620344480)* --------- 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 3bcd5df commit 3e34794

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
""" pyplots.ai
2+
slider-control-basic: Interactive Plot with Slider Control
3+
Library: altair 6.0.0 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-31
5+
"""
6+
7+
import altair as alt
8+
import numpy as np
9+
import pandas as pd
10+
11+
12+
# Data - Monthly sales data across multiple years
13+
np.random.seed(42)
14+
years = [2019, 2020, 2021, 2022, 2023]
15+
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
16+
17+
data = []
18+
base_sales = 100
19+
for year in years:
20+
# Different growth patterns per year
21+
year_factor = 1 + (year - 2019) * 0.15
22+
for i, month in enumerate(months):
23+
# Seasonal pattern with noise
24+
seasonal = np.sin(i * np.pi / 6) * 20 + 10
25+
sales = base_sales * year_factor + seasonal + np.random.normal(0, 10)
26+
data.append({"Year": year, "Month": month, "Month_Num": i + 1, "Sales": max(0, sales)})
27+
28+
df = pd.DataFrame(data)
29+
30+
# Create slider binding for year selection
31+
year_slider = alt.binding_range(min=2019, max=2023, step=1, name="Year: ")
32+
year_selection = alt.selection_point(fields=["Year"], bind=year_slider, value=2023)
33+
34+
# Bar chart showing monthly sales for selected year
35+
chart = (
36+
alt.Chart(df)
37+
.mark_bar(size=40, color="#306998", opacity=0.8)
38+
.encode(
39+
x=alt.X("Month:N", sort=months, axis=alt.Axis(labelFontSize=18, titleFontSize=22, labelAngle=0), title="Month"),
40+
y=alt.Y(
41+
"Sales:Q",
42+
axis=alt.Axis(labelFontSize=18, titleFontSize=22),
43+
title="Sales (thousands $)",
44+
scale=alt.Scale(domain=[0, 200]),
45+
),
46+
tooltip=[
47+
alt.Tooltip("Year:O", title="Year"),
48+
alt.Tooltip("Month:N", title="Month"),
49+
alt.Tooltip("Sales:Q", title="Sales", format=".1f"),
50+
],
51+
)
52+
.add_params(year_selection)
53+
.transform_filter(year_selection)
54+
.properties(
55+
width=1600,
56+
height=900,
57+
title=alt.Title(
58+
"slider-control-basic · altair · pyplots.ai",
59+
fontSize=28,
60+
anchor="middle",
61+
subtitle="Use the Year slider to filter monthly sales data",
62+
subtitleFontSize=18,
63+
),
64+
)
65+
.configure_axis(gridColor="#cccccc", gridOpacity=0.3, domainColor="#333333")
66+
.configure_view(strokeWidth=0)
67+
)
68+
69+
# Save as PNG (static snapshot at default year)
70+
chart.save("plot.png", scale_factor=3.0)
71+
72+
# Save as HTML (interactive version with working slider)
73+
chart.save("plot.html")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library: altair
2+
specification_id: slider-control-basic
3+
created: '2025-12-31T13:54:14Z'
4+
updated: '2025-12-31T14:05:06Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20620344480
7+
issue: 3071
8+
python_version: 3.13.11
9+
library_version: 6.0.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/slider-control-basic/altair/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/slider-control-basic/altair/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/slider-control-basic/altair/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent use of Altair declarative selection and binding system for the slider
17+
- Clean readable code following KISS principles
18+
- Realistic business scenario with seasonal sales patterns
19+
- Good visual design with appropriate font sizes and color choice
20+
- Both PNG and HTML outputs provided for static and interactive use
21+
weaknesses:
22+
- Y-axis scale starts at 0 but data ranges from 140-190 creating unused space at
23+
bottom
24+
- Current slider value not prominently displayed near the slider as spec suggests

0 commit comments

Comments
 (0)