|
| 1 | +""" pyplots.ai |
| 2 | +line-interactive: Interactive Line Chart with Hover and Zoom |
| 3 | +Library: seaborn 0.13.2 | Python 3.13.11 |
| 4 | +Quality: 90/100 | Created: 2025-12-30 |
| 5 | +""" |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +import seaborn as sns |
| 11 | + |
| 12 | + |
| 13 | +# Data - Daily temperature readings with seasonal patterns (realistic weather scenario) |
| 14 | +np.random.seed(42) |
| 15 | +n_points = 180 # ~6 months of daily data |
| 16 | + |
| 17 | +# Generate datetime index |
| 18 | +dates = pd.date_range("2024-01-01", periods=n_points, freq="D") |
| 19 | + |
| 20 | +# Generate realistic temperature data with seasonal pattern |
| 21 | +base_temp = 15 # Base temperature in Celsius |
| 22 | +seasonal_pattern = 12 * np.sin(2 * np.pi * np.arange(n_points) / 365 - np.pi / 2) # Seasonal cycle |
| 23 | +weekly_variation = 3 * np.sin(2 * np.pi * np.arange(n_points) / 7) # Weekly variation |
| 24 | +noise = np.random.normal(0, 2.5, n_points) |
| 25 | + |
| 26 | +# Combine patterns for realistic temperature |
| 27 | +temperature = base_temp + seasonal_pattern + weekly_variation + noise |
| 28 | + |
| 29 | +# Add some extreme events (heat waves and cold snaps) |
| 30 | +heat_wave_indices = [85, 86, 87, 88, 89] # Late March heat wave |
| 31 | +cold_snap_indices = [25, 26, 27] # Late January cold snap |
| 32 | +for idx in heat_wave_indices: |
| 33 | + temperature[idx] += np.random.uniform(6, 10) |
| 34 | +for idx in cold_snap_indices: |
| 35 | + temperature[idx] -= np.random.uniform(5, 8) |
| 36 | + |
| 37 | +# Create DataFrame for seaborn |
| 38 | +df = pd.DataFrame({"Date": dates, "Temperature": temperature, "Day": np.arange(n_points)}) |
| 39 | + |
| 40 | +# Set seaborn style (white, not whitegrid - we'll add custom subtle grid) |
| 41 | +sns.set_theme(style="white") |
| 42 | + |
| 43 | +# Create the figure with two axes: main plot and range selector |
| 44 | +fig, (ax, ax_range) = plt.subplots(2, 1, figsize=(16, 9), height_ratios=[5, 1], sharex=False) |
| 45 | +fig.subplots_adjust(hspace=0.15) |
| 46 | + |
| 47 | +# Main line plot using seaborn |
| 48 | +sns.lineplot(data=df, x="Date", y="Temperature", color="#306998", linewidth=2.5, ax=ax, label="Daily Temperature") |
| 49 | + |
| 50 | +# Add scatter points for hover targets (every 10th point) |
| 51 | +scatter_df = df.iloc[::10].copy() |
| 52 | +sns.scatterplot( |
| 53 | + data=scatter_df, |
| 54 | + x="Date", |
| 55 | + y="Temperature", |
| 56 | + color="#306998", |
| 57 | + s=150, |
| 58 | + alpha=0.8, |
| 59 | + edgecolor="white", |
| 60 | + linewidth=1.5, |
| 61 | + ax=ax, |
| 62 | + zorder=3, |
| 63 | + label="Data Points", |
| 64 | +) |
| 65 | + |
| 66 | +# Range selector subplot - simplified overview of the data |
| 67 | +sns.lineplot(data=df, x="Date", y="Temperature", color="#306998", linewidth=1.5, ax=ax_range, legend=False) |
| 68 | +ax_range.set_ylabel("") |
| 69 | +ax_range.set_xlabel("Drag to Select Range", fontsize=14) |
| 70 | +ax_range.tick_params(axis="y", labelsize=10) |
| 71 | +ax_range.tick_params(axis="x", labelsize=12) |
| 72 | +ax_range.set_title("Range Selector", fontsize=14, fontweight="bold", loc="left") |
| 73 | + |
| 74 | +# Add span selector for range selection (demonstrates interactive range selection) |
| 75 | +# Highlight current selection on the range selector |
| 76 | +selected_start, selected_end = 40, 100 |
| 77 | +ax_range.axvspan(dates[selected_start], dates[selected_end], alpha=0.3, color="#FFD43B", zorder=2) |
| 78 | +ax_range.annotate( |
| 79 | + "Selected Range", |
| 80 | + xy=(dates[(selected_start + selected_end) // 2], ax_range.get_ylim()[1]), |
| 81 | + xytext=(0, -5), |
| 82 | + textcoords="offset points", |
| 83 | + fontsize=11, |
| 84 | + ha="center", |
| 85 | + va="top", |
| 86 | + color="#306998", |
| 87 | + fontweight="bold", |
| 88 | +) |
| 89 | + |
| 90 | +# Highlight heat wave with visible markers and annotation |
| 91 | +heat_wave_df = df.iloc[heat_wave_indices] |
| 92 | +ax.scatter( |
| 93 | + heat_wave_df["Date"], |
| 94 | + heat_wave_df["Temperature"], |
| 95 | + color="#E63946", |
| 96 | + s=250, |
| 97 | + edgecolors="#306998", |
| 98 | + linewidths=2.5, |
| 99 | + zorder=5, |
| 100 | + marker="^", |
| 101 | +) |
| 102 | +ax.annotate( |
| 103 | + f"Heat Wave: {heat_wave_df['Temperature'].max():.1f}°C", |
| 104 | + xy=(dates[87], temperature[87]), |
| 105 | + xytext=(0, 25), |
| 106 | + textcoords="offset points", |
| 107 | + fontsize=13, |
| 108 | + fontweight="bold", |
| 109 | + color="white", |
| 110 | + ha="center", |
| 111 | + bbox={"boxstyle": "round,pad=0.4", "facecolor": "#E63946", "alpha": 0.95, "edgecolor": "#306998", "linewidth": 1.5}, |
| 112 | + arrowprops={"arrowstyle": "-", "color": "#E63946", "lw": 2}, |
| 113 | +) |
| 114 | + |
| 115 | +# Highlight cold snap with visible markers and annotation |
| 116 | +cold_snap_df = df.iloc[cold_snap_indices] |
| 117 | +ax.scatter( |
| 118 | + cold_snap_df["Date"], |
| 119 | + cold_snap_df["Temperature"], |
| 120 | + color="#1E88E5", |
| 121 | + s=250, |
| 122 | + edgecolors="#306998", |
| 123 | + linewidths=2.5, |
| 124 | + zorder=5, |
| 125 | + marker="v", |
| 126 | +) |
| 127 | +ax.annotate( |
| 128 | + f"Cold Snap: {cold_snap_df['Temperature'].min():.1f}°C", |
| 129 | + xy=(dates[26], temperature[26]), |
| 130 | + xytext=(0, -35), |
| 131 | + textcoords="offset points", |
| 132 | + fontsize=13, |
| 133 | + fontweight="bold", |
| 134 | + color="white", |
| 135 | + ha="center", |
| 136 | + bbox={"boxstyle": "round,pad=0.4", "facecolor": "#1E88E5", "alpha": 0.95, "edgecolor": "#306998", "linewidth": 1.5}, |
| 137 | + arrowprops={"arrowstyle": "-", "color": "#1E88E5", "lw": 2}, |
| 138 | +) |
| 139 | + |
| 140 | +# Add average temperature reference line |
| 141 | +avg_temp = np.mean(temperature) |
| 142 | +ax.axhline(y=avg_temp, color="#808080", linestyle="--", linewidth=2, alpha=0.7, label=f"Average: {avg_temp:.1f}°C") |
| 143 | + |
| 144 | +# Style the plot |
| 145 | +ax.set_xlabel("Date", fontsize=20) |
| 146 | +ax.set_ylabel("Temperature (°C)", fontsize=20) |
| 147 | +ax.set_title("line-interactive · seaborn · pyplots.ai", fontsize=24, fontweight="bold", pad=15) |
| 148 | + |
| 149 | +# Configure tick parameters |
| 150 | +ax.tick_params(axis="both", labelsize=16) |
| 151 | + |
| 152 | +# Format x-axis for better date display |
| 153 | +fig.autofmt_xdate(rotation=30) |
| 154 | + |
| 155 | +# Add subtle grid (using white theme, so no double grid effect) |
| 156 | +ax.grid(True, alpha=0.25, linestyle="--") |
| 157 | +ax_range.grid(True, alpha=0.2, linestyle="-") |
| 158 | + |
| 159 | +# Add legend |
| 160 | +ax.legend(fontsize=14, loc="upper right", framealpha=0.95) |
| 161 | + |
| 162 | +# Add interactive controls hint |
| 163 | +fig.text( |
| 164 | + 0.5, |
| 165 | + 0.01, |
| 166 | + "Interactive Controls: Hover points for values • Scroll to zoom • Click-drag to pan • Home to reset", |
| 167 | + ha="center", |
| 168 | + va="bottom", |
| 169 | + fontsize=12, |
| 170 | + color="#555555", |
| 171 | + style="italic", |
| 172 | + bbox={"boxstyle": "round,pad=0.4", "facecolor": "#f0f0f0", "alpha": 0.9, "edgecolor": "#cccccc"}, |
| 173 | +) |
| 174 | + |
| 175 | +# Ensure proper layout with extra bottom margin for footer |
| 176 | +plt.tight_layout() |
| 177 | +plt.subplots_adjust(bottom=0.15) |
| 178 | + |
| 179 | +# Demonstrate hover tooltip with a static annotation |
| 180 | +demo_idx = 12 # Show tooltip on a representative point |
| 181 | +demo_x, demo_y = dates[demo_idx * 10], temperature[demo_idx * 10] |
| 182 | +demo_date_str = dates[demo_idx * 10].strftime("%b %d, %Y") |
| 183 | +ax.annotate( |
| 184 | + f"Date: {demo_date_str}\nTemp: {demo_y:.1f}°C", |
| 185 | + xy=(demo_x, demo_y), |
| 186 | + xytext=(35, 35), |
| 187 | + textcoords="offset points", |
| 188 | + fontsize=14, |
| 189 | + fontweight="bold", |
| 190 | + color="#306998", |
| 191 | + bbox={"boxstyle": "round,pad=0.5", "facecolor": "#FFD43B", "alpha": 0.95, "edgecolor": "#306998", "linewidth": 2}, |
| 192 | + arrowprops={"arrowstyle": "->", "color": "#306998", "lw": 2, "connectionstyle": "arc3,rad=0.2"}, |
| 193 | + zorder=10, |
| 194 | +) |
| 195 | + |
| 196 | +# Save the plot |
| 197 | +plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
0 commit comments