|
1 | 1 | """ anyplot.ai |
2 | 2 | line-basic: Basic Line Plot |
3 | | -Library: plotly 6.7.0 | Python 3.14.4 |
4 | | -Quality: 82/100 | Updated: 2026-04-29 |
| 3 | +Library: plotly 6.7.0 | Python 3.13.13 |
| 4 | +Quality: 92/100 | Updated: 2026-04-29 |
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 |
|
11 | | -# Data - Monthly temperature readings |
| 13 | +# Theme tokens |
| 14 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 15 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 16 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 17 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 18 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 19 | +GRID = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)" |
| 20 | +BRAND = "#009E73" |
| 21 | +BRAND_FILL = "rgba(0,158,115,0.12)" |
| 22 | + |
| 23 | +# Data - Monthly temperature readings (seasonal pattern) |
12 | 24 | np.random.seed(42) |
13 | 25 | months = np.arange(1, 13) |
14 | | -# Simulate temperature pattern (cold winter, warm summer) |
| 26 | +month_labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] |
15 | 27 | temperature = 15 + 12 * np.sin((months - 4) * np.pi / 6) + np.random.randn(12) * 1.5 |
16 | 28 |
|
17 | | -# Create figure |
| 29 | +peak_idx = int(np.argmax(temperature)) |
| 30 | + |
| 31 | +# Plot |
18 | 32 | fig = go.Figure() |
19 | 33 |
|
20 | 34 | fig.add_trace( |
21 | 35 | go.Scatter( |
22 | 36 | x=months, |
23 | 37 | y=temperature, |
24 | 38 | mode="lines+markers", |
25 | | - line={"color": "#306998", "width": 5}, |
26 | | - marker={"size": 18, "color": "#306998"}, |
27 | | - hovertemplate="Month: %{x}<br>Temperature: %{y:.1f}°C<extra></extra>", |
| 39 | + line={"color": BRAND, "width": 5}, |
| 40 | + marker={"size": 18, "color": BRAND}, |
| 41 | + fill="tozeroy", |
| 42 | + fillcolor=BRAND_FILL, |
| 43 | + hovertemplate="%{customdata}<br>%{y:.1f}°C<extra></extra>", |
| 44 | + customdata=month_labels, |
28 | 45 | ) |
29 | 46 | ) |
30 | 47 |
|
31 | | -# Layout |
| 48 | +# Peak annotation for visual storytelling |
| 49 | +fig.add_annotation( |
| 50 | + x=months[peak_idx], |
| 51 | + y=temperature[peak_idx], |
| 52 | + text=f"Peak: {temperature[peak_idx]:.1f}°C", |
| 53 | + showarrow=True, |
| 54 | + arrowhead=2, |
| 55 | + arrowcolor=INK_SOFT, |
| 56 | + arrowwidth=2, |
| 57 | + ax=40, |
| 58 | + ay=-50, |
| 59 | + font={"size": 20, "color": INK}, |
| 60 | + bgcolor=ELEVATED_BG, |
| 61 | + bordercolor=INK_SOFT, |
| 62 | + borderwidth=1, |
| 63 | + borderpad=6, |
| 64 | +) |
| 65 | + |
32 | 66 | fig.update_layout( |
33 | | - title={"text": "line-basic · plotly · pyplots.ai", "font": {"size": 40}, "x": 0.5, "xanchor": "center"}, |
| 67 | + paper_bgcolor=PAGE_BG, |
| 68 | + plot_bgcolor=PAGE_BG, |
| 69 | + font={"color": INK}, |
| 70 | + title={ |
| 71 | + "text": "line-basic · plotly · anyplot.ai", |
| 72 | + "font": {"size": 36, "color": INK}, |
| 73 | + "x": 0.5, |
| 74 | + "xanchor": "center", |
| 75 | + }, |
34 | 76 | xaxis={ |
35 | | - "title": {"text": "Month", "font": {"size": 36}}, |
36 | | - "tickfont": {"size": 28}, |
| 77 | + "title": {"text": "Month", "font": {"size": 28, "color": INK}}, |
| 78 | + "tickfont": {"size": 22, "color": INK_SOFT}, |
37 | 79 | "tickmode": "array", |
38 | 80 | "tickvals": months, |
39 | | - "ticktext": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], |
| 81 | + "ticktext": month_labels, |
40 | 82 | "showgrid": True, |
41 | 83 | "gridwidth": 1, |
42 | | - "gridcolor": "rgba(0,0,0,0.1)", |
| 84 | + "gridcolor": GRID, |
| 85 | + "showline": True, |
| 86 | + "linecolor": INK_SOFT, |
| 87 | + "mirror": False, |
| 88 | + "zerolinecolor": GRID, |
| 89 | + "showspikes": True, |
| 90 | + "spikemode": "across", |
| 91 | + "spikethickness": 1, |
| 92 | + "spikedash": "dot", |
| 93 | + "spikecolor": INK_SOFT, |
43 | 94 | }, |
44 | 95 | yaxis={ |
45 | | - "title": {"text": "Temperature (°C)", "font": {"size": 36}}, |
46 | | - "tickfont": {"size": 28}, |
| 96 | + "title": {"text": "Temperature (°C)", "font": {"size": 28, "color": INK}}, |
| 97 | + "tickfont": {"size": 22, "color": INK_SOFT}, |
47 | 98 | "showgrid": True, |
48 | 99 | "gridwidth": 1, |
49 | | - "gridcolor": "rgba(0,0,0,0.1)", |
| 100 | + "gridcolor": GRID, |
| 101 | + "showline": True, |
| 102 | + "linecolor": INK_SOFT, |
| 103 | + "mirror": False, |
| 104 | + "zerolinecolor": GRID, |
| 105 | + "rangemode": "tozero", |
| 106 | + "showspikes": True, |
| 107 | + "spikemode": "across", |
| 108 | + "spikethickness": 1, |
| 109 | + "spikedash": "dot", |
| 110 | + "spikecolor": INK_SOFT, |
50 | 111 | }, |
51 | | - template="plotly_white", |
52 | | - margin={"t": 120, "b": 100, "l": 120, "r": 50}, |
| 112 | + hovermode="x", |
| 113 | + margin={"t": 120, "b": 100, "l": 120, "r": 60}, |
53 | 114 | ) |
54 | 115 |
|
55 | 116 | # Save |
56 | | -fig.write_image("plot.png", width=1600, height=900, scale=3) |
57 | | -fig.write_html("plot.html") |
| 117 | +fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3) |
| 118 | +fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn") |
0 commit comments