|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | line-timeseries: Time Series Line Plot |
3 | | -Library: plotly 6.5.0 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-26 |
| 3 | +Library: plotly 6.7.0 | Python 3.13.13 |
| 4 | +Quality: 90/100 | Updated: 2026-05-09 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import numpy as np |
8 | 10 | import pandas as pd |
9 | 11 | import plotly.graph_objects as go |
10 | 12 |
|
11 | 13 |
|
12 | | -# Data - Daily temperature readings over one year |
| 14 | +# Theme tokens |
| 15 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 16 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 17 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 18 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 19 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 20 | +GRID = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)" |
| 21 | +BRAND = "#009E73" # Okabe-Ito position 1 |
| 22 | + |
| 23 | +# Data - Stock prices with uptrend |
13 | 24 | np.random.seed(42) |
14 | 25 | dates = pd.date_range(start="2024-01-01", end="2024-12-31", freq="D") |
| 26 | +days = np.arange(len(dates)) |
| 27 | +trend = 100 + 0.15 * days |
| 28 | +volatility = 5 * np.sin(2 * np.pi * days / 60) |
| 29 | +noise = np.random.randn(len(dates)) * 2 |
| 30 | +prices = trend + volatility + noise |
15 | 31 |
|
16 | | -# Create realistic temperature pattern with seasonal variation |
17 | | -day_of_year = np.arange(len(dates)) |
18 | | -seasonal = 15 * np.sin(2 * np.pi * (day_of_year - 80) / 365) # Peak around late July |
19 | | -baseline = 12 # Average temperature |
20 | | -noise = np.random.randn(len(dates)) * 3 |
21 | | -temperatures = baseline + seasonal + noise |
22 | | - |
23 | | -df = pd.DataFrame({"date": dates, "temperature": temperatures}) |
| 32 | +df = pd.DataFrame({"date": dates, "price": prices}) |
24 | 33 |
|
25 | 34 | # Create plot |
26 | 35 | fig = go.Figure() |
27 | 36 |
|
28 | 37 | fig.add_trace( |
29 | 38 | go.Scatter( |
30 | 39 | x=df["date"], |
31 | | - y=df["temperature"], |
| 40 | + y=df["price"], |
32 | 41 | mode="lines", |
33 | | - line={"color": "#306998", "width": 2.5}, |
34 | | - name="Temperature", |
35 | | - hovertemplate="Date: %{x|%b %d, %Y}<br>Temperature: %{y:.1f}°C<extra></extra>", |
| 42 | + line={"color": BRAND, "width": 3}, |
| 43 | + name="Stock Price", |
| 44 | + hovertemplate="Date: %{x|%b %d, %Y}<br>Price: $%{y:.2f}<extra></extra>", |
36 | 45 | ) |
37 | 46 | ) |
38 | 47 |
|
39 | 48 | # Layout for 4800x2700 px |
40 | 49 | fig.update_layout( |
41 | 50 | title={ |
42 | | - "text": "Daily Temperature 2024 · line-timeseries · plotly · pyplots.ai", |
43 | | - "font": {"size": 32}, |
| 51 | + "text": "line-timeseries · plotly · anyplot.ai", |
| 52 | + "font": {"size": 28, "color": INK}, |
44 | 53 | "x": 0.5, |
45 | 54 | "xanchor": "center", |
46 | 55 | }, |
47 | 56 | xaxis={ |
48 | | - "title": {"text": "Date", "font": {"size": 24}}, |
49 | | - "tickfont": {"size": 18}, |
| 57 | + "title": {"text": "Date", "font": {"size": 22, "color": INK}}, |
| 58 | + "tickfont": {"size": 18, "color": INK_SOFT}, |
50 | 59 | "tickformat": "%b %Y", |
51 | 60 | "dtick": "M1", |
52 | | - "gridcolor": "rgba(128, 128, 128, 0.3)", |
| 61 | + "gridcolor": GRID, |
53 | 62 | "gridwidth": 1, |
54 | 63 | "showgrid": True, |
| 64 | + "linecolor": INK_SOFT, |
| 65 | + "zerolinecolor": INK_SOFT, |
| 66 | + "rangeslider": {"visible": True, "thickness": 0.05}, |
55 | 67 | }, |
56 | 68 | yaxis={ |
57 | | - "title": {"text": "Temperature (°C)", "font": {"size": 24}}, |
58 | | - "tickfont": {"size": 18}, |
59 | | - "gridcolor": "rgba(128, 128, 128, 0.3)", |
| 69 | + "title": {"text": "Stock Price ($)", "font": {"size": 22, "color": INK}}, |
| 70 | + "tickfont": {"size": 18, "color": INK_SOFT}, |
| 71 | + "gridcolor": GRID, |
60 | 72 | "gridwidth": 1, |
61 | 73 | "showgrid": True, |
| 74 | + "linecolor": INK_SOFT, |
| 75 | + "zerolinecolor": INK_SOFT, |
62 | 76 | }, |
63 | | - template="plotly_white", |
64 | | - plot_bgcolor="white", |
65 | | - paper_bgcolor="white", |
| 77 | + paper_bgcolor=PAGE_BG, |
| 78 | + plot_bgcolor=PAGE_BG, |
| 79 | + font={"color": INK}, |
66 | 80 | margin={"l": 100, "r": 80, "t": 120, "b": 100}, |
67 | 81 | showlegend=False, |
| 82 | + hovermode="x unified", |
68 | 83 | ) |
69 | 84 |
|
70 | 85 | # Save as PNG (4800x2700 px) and HTML |
71 | | -fig.write_image("plot.png", width=1600, height=900, scale=3) |
72 | | -fig.write_html("plot.html", include_plotlyjs="cdn") |
| 86 | +fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3) |
| 87 | +fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn") |
0 commit comments