Skip to content

Commit 2af41e9

Browse files
feat(seaborn): implement slider-control-basic (#3097)
## Implementation: `slider-control-basic` - seaborn Implements the **seaborn** version of `slider-control-basic`. **File:** `plots/slider-control-basic/implementations/seaborn.py` **Parent Issue:** #3071 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20620342647)* --------- 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 18dd0f4 commit 2af41e9

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
""" pyplots.ai
2+
slider-control-basic: Interactive Plot with Slider Control
3+
Library: seaborn 0.13.2 | Python 3.13.11
4+
Quality: 90/100 | Created: 2025-12-31
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
import seaborn as sns
10+
from matplotlib.widgets import Slider
11+
12+
13+
# Data - Sales data across multiple years
14+
np.random.seed(42)
15+
years = np.arange(2018, 2025)
16+
17+
# Generate sales data for each year with realistic patterns
18+
sales_data = {}
19+
for year in years:
20+
base = 50000 + (year - 2018) * 5000 # Growing trend over years
21+
seasonal = 10000 * np.sin(np.linspace(0, 2 * np.pi, 12)) # Seasonal pattern
22+
noise = np.random.normal(0, 3000, 12)
23+
sales_data[year] = base + seasonal + noise
24+
25+
# Create figure with space for slider
26+
fig, ax = plt.subplots(figsize=(16, 9))
27+
plt.subplots_adjust(bottom=0.22)
28+
29+
# Initial year to display
30+
initial_year = 2022
31+
32+
# Style settings
33+
sns.set_style("whitegrid")
34+
35+
# Month names for x-axis
36+
month_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
37+
38+
# Initial plot using seaborn
39+
bars = sns.barplot(x=month_names, y=sales_data[initial_year], ax=ax, color="#306998", edgecolor="white", linewidth=2)
40+
41+
# Styling
42+
ax.set_xlabel("Month", fontsize=20)
43+
ax.set_ylabel("Sales ($)", fontsize=20)
44+
ax.set_title("slider-control-basic · seaborn · pyplots.ai", fontsize=24, fontweight="bold")
45+
ax.tick_params(axis="both", labelsize=16)
46+
ax.set_ylim(0, 100000)
47+
48+
# Format y-axis with dollar signs
49+
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: f"${x / 1000:.0f}K"))
50+
51+
# Store text annotations for updating
52+
value_texts = []
53+
for bar, val in zip(ax.patches, sales_data[initial_year], strict=True):
54+
txt = ax.text(
55+
bar.get_x() + bar.get_width() / 2,
56+
bar.get_height() + 1500,
57+
f"${val / 1000:.0f}K",
58+
ha="center",
59+
va="bottom",
60+
fontsize=12,
61+
fontweight="bold",
62+
)
63+
value_texts.append(txt)
64+
65+
# Create slider axis with proper positioning
66+
slider_ax = plt.axes([0.2, 0.06, 0.6, 0.04])
67+
year_slider = Slider(
68+
ax=slider_ax, label="Year", valmin=2018, valmax=2024, valinit=initial_year, valstep=1, color="#306998"
69+
)
70+
year_slider.label.set_fontsize(18)
71+
year_slider.valtext.set_fontsize(18)
72+
73+
74+
# Slider update callback function
75+
def update_plot(val):
76+
year = int(year_slider.val)
77+
new_data = sales_data[year]
78+
79+
# Update bar heights
80+
for bar, height in zip(ax.patches, new_data, strict=True):
81+
bar.set_height(height)
82+
83+
# Update value labels
84+
for txt, bar, val in zip(value_texts, ax.patches, new_data, strict=True):
85+
txt.set_position((bar.get_x() + bar.get_width() / 2, bar.get_height() + 1500))
86+
txt.set_text(f"${val / 1000:.0f}K")
87+
88+
fig.canvas.draw_idle()
89+
90+
91+
# Connect slider to update function
92+
year_slider.on_changed(update_plot)
93+
94+
# Add instructional text for slider interaction
95+
fig.text(
96+
0.5,
97+
0.015,
98+
"Drag the slider to explore monthly sales data from 2018-2024",
99+
ha="center",
100+
fontsize=16,
101+
style="italic",
102+
color="#666666",
103+
)
104+
105+
plt.savefig("plot.png", dpi=300, bbox_inches="tight", facecolor="white")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library: seaborn
2+
specification_id: slider-control-basic
3+
created: '2025-12-31T13:53:48Z'
4+
updated: '2025-12-31T14:27:35Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20620342647
7+
issue: 3071
8+
python_version: 3.13.11
9+
library_version: 0.13.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/slider-control-basic/seaborn/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/slider-control-basic/seaborn/plot_thumb.png
12+
preview_html: null
13+
quality_score: 90
14+
review:
15+
strengths:
16+
- Excellent implementation of slider interactivity using matplotlib.widgets.Slider
17+
with seaborn bar plot
18+
- Clean data generation with realistic seasonal sales patterns and year-over-year
19+
growth
20+
- Value labels above each bar provide excellent data readability
21+
- Proper font sizing throughout (24pt title, 20pt axis labels, 16pt ticks, 18pt
22+
slider labels)
23+
- Y-axis formatted with dollar signs and K notation for clarity
24+
- Instructional text helps users understand the interactive functionality
25+
- Good use of seaborn whitegrid style for professional appearance
26+
weaknesses:
27+
- Grid/legend score slightly reduced as whitegrid could be more subtle for a bar
28+
chart
29+
- Layout has extra bottom margin for slider which slightly reduces plot area proportion

0 commit comments

Comments
 (0)