|
| 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") |
0 commit comments