|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | stem-basic: Basic Stem Plot |
3 | | -Library: plotly 6.5.0 | Python 3.13.11 |
4 | | -Quality: 93/100 | Created: 2025-12-23 |
| 3 | +Library: plotly 6.7.0 | Python 3.13.13 |
| 4 | +Quality: 90/100 | Updated: 2026-04-30 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import numpy as np |
8 | 10 | import plotly.graph_objects as go |
9 | 11 |
|
10 | 12 |
|
| 13 | +# Theme tokens |
| 14 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 15 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 16 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 17 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 18 | +GRID = "rgba(26,26,23,0.20)" if THEME == "light" else "rgba(240,239,232,0.20)" |
| 19 | +BRAND = "#009E73" # Okabe-Ito position 1 |
| 20 | + |
11 | 21 | # Data - Discrete signal samples (damped oscillation) |
12 | 22 | np.random.seed(42) |
13 | 23 | x = np.arange(0, 30) |
14 | 24 | y = np.exp(-x / 10) * np.cos(x * 0.8) + np.random.randn(30) * 0.05 |
15 | 25 |
|
16 | | -# Create figure |
| 26 | +# Plot |
17 | 27 | fig = go.Figure() |
18 | 28 |
|
19 | | -# Add baseline at y=0 |
| 29 | +# Baseline at y=0 |
20 | 30 | fig.add_trace( |
21 | 31 | go.Scatter( |
22 | 32 | x=[x.min() - 0.5, x.max() + 0.5], |
23 | 33 | y=[0, 0], |
24 | 34 | mode="lines", |
25 | | - line={"color": "#333333", "width": 2}, |
| 35 | + line={"color": INK_SOFT, "width": 2}, |
26 | 36 | showlegend=False, |
27 | 37 | hoverinfo="skip", |
28 | 38 | ) |
29 | 39 | ) |
30 | 40 |
|
31 | | -# Add stems (vertical lines from baseline to data points) |
| 41 | +# Stems (vertical lines from baseline to data points) |
32 | 42 | for xi, yi in zip(x, y, strict=True): |
33 | 43 | fig.add_trace( |
34 | 44 | go.Scatter( |
35 | | - x=[xi, xi], |
36 | | - y=[0, yi], |
37 | | - mode="lines", |
38 | | - line={"color": "#306998", "width": 2}, |
39 | | - showlegend=False, |
40 | | - hoverinfo="skip", |
| 45 | + x=[xi, xi], y=[0, yi], mode="lines", line={"color": BRAND, "width": 2}, showlegend=False, hoverinfo="skip" |
41 | 46 | ) |
42 | 47 | ) |
43 | 48 |
|
44 | | -# Add markers at the top of each stem |
| 49 | +# Markers at the top of each stem |
45 | 50 | fig.add_trace( |
46 | 51 | go.Scatter( |
47 | 52 | x=x, |
48 | 53 | y=y, |
49 | 54 | mode="markers", |
50 | | - marker={"color": "#306998", "size": 16, "line": {"color": "white", "width": 2}}, |
| 55 | + marker={"color": BRAND, "size": 16, "line": {"color": PAGE_BG, "width": 2}}, |
51 | 56 | showlegend=False, |
52 | 57 | hovertemplate="Sample: %{x}<br>Amplitude: %{y:.3f}<extra></extra>", |
53 | 58 | ) |
54 | 59 | ) |
55 | 60 |
|
56 | | -# Update layout for 4800x2700 px |
| 61 | +# Style |
57 | 62 | fig.update_layout( |
58 | | - title={"text": "stem-basic · plotly · pyplots.ai", "font": {"size": 42}, "x": 0.5, "xanchor": "center"}, |
| 63 | + paper_bgcolor=PAGE_BG, |
| 64 | + plot_bgcolor=PAGE_BG, |
| 65 | + font={"color": INK}, |
| 66 | + title={ |
| 67 | + "text": "stem-basic · plotly · anyplot.ai", |
| 68 | + "font": {"size": 42, "color": INK}, |
| 69 | + "x": 0.5, |
| 70 | + "xanchor": "center", |
| 71 | + }, |
59 | 72 | xaxis={ |
60 | | - "title": {"text": "Sample Index", "font": {"size": 36}}, |
61 | | - "tickfont": {"size": 28}, |
62 | | - "gridcolor": "rgba(0,0,0,0.1)", |
| 73 | + "title": {"text": "Sample Index (n)", "font": {"size": 36, "color": INK}}, |
| 74 | + "tickfont": {"size": 28, "color": INK_SOFT}, |
| 75 | + "gridcolor": GRID, |
63 | 76 | "gridwidth": 1, |
64 | 77 | "zeroline": False, |
| 78 | + "linecolor": INK_SOFT, |
65 | 79 | }, |
66 | 80 | yaxis={ |
67 | | - "title": {"text": "Amplitude", "font": {"size": 36}}, |
68 | | - "tickfont": {"size": 28}, |
69 | | - "gridcolor": "rgba(0,0,0,0.1)", |
| 81 | + "title": {"text": "Amplitude (a.u.)", "font": {"size": 36, "color": INK}}, |
| 82 | + "tickfont": {"size": 28, "color": INK_SOFT}, |
| 83 | + "gridcolor": GRID, |
70 | 84 | "gridwidth": 1, |
71 | 85 | "zeroline": False, |
| 86 | + "linecolor": INK_SOFT, |
72 | 87 | }, |
73 | | - template="plotly_white", |
74 | 88 | margin={"l": 120, "r": 60, "t": 130, "b": 110}, |
75 | 89 | showlegend=False, |
76 | 90 | ) |
77 | 91 |
|
78 | | -# Save as PNG (4800x2700 px) |
79 | | -fig.write_image("plot.png", width=1600, height=900, scale=3) |
80 | | - |
81 | | -# Save interactive HTML |
82 | | -fig.write_html("plot.html") |
| 92 | +# Save |
| 93 | +fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3) |
| 94 | +fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn") |
0 commit comments