|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | line-timeseries: Time Series Line Plot |
3 | | -Library: altair 6.0.0 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-26 |
| 3 | +Library: altair 6.1.0 | Python 3.13.13 |
| 4 | +Quality: 92/100 | Updated: 2026-05-09 |
5 | 5 | """ |
6 | 6 |
|
7 | | -import altair as alt |
8 | | -import numpy as np |
9 | | -import pandas as pd |
| 7 | +import os |
| 8 | +import sys |
10 | 9 |
|
11 | 10 |
|
| 11 | +# Remove the current directory from sys.path to avoid importing altair.py |
| 12 | +sys.path = [p for p in sys.path if p != "" and not p.endswith("python")] |
| 13 | + |
| 14 | +import altair as alt # noqa: E402 |
| 15 | +import numpy as np # noqa: E402 |
| 16 | +import pandas as pd # noqa: E402 |
| 17 | + |
| 18 | + |
| 19 | +# Theme tokens |
| 20 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 21 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 22 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 23 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 24 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 25 | +BRAND = "#009E73" # Okabe-Ito position 1 |
| 26 | + |
12 | 27 | # Data - Daily stock prices over one year |
13 | 28 | np.random.seed(42) |
14 | 29 |
|
|
29 | 44 | # Create time series line chart |
30 | 45 | chart = ( |
31 | 46 | alt.Chart(df) |
32 | | - .mark_line( |
33 | | - strokeWidth=3, |
34 | | - color="#306998", # Python Blue |
35 | | - ) |
| 47 | + .mark_line(strokeWidth=3, color=BRAND, point=True, size=150) |
36 | 48 | .encode( |
37 | 49 | x=alt.X( |
38 | 50 | "date:T", |
39 | 51 | title="Date", |
40 | 52 | axis=alt.Axis( |
41 | | - format="%b %Y", # Month Year format |
| 53 | + format="%b %Y", |
42 | 54 | labelFontSize=18, |
43 | 55 | titleFontSize=22, |
44 | 56 | labelAngle=-45, |
45 | 57 | tickCount=12, |
| 58 | + labelColor=INK_SOFT, |
| 59 | + titleColor=INK, |
| 60 | + domainColor=INK_SOFT, |
| 61 | + gridColor=INK, |
| 62 | + gridOpacity=0.10, |
46 | 63 | ), |
47 | 64 | ), |
48 | 65 | y=alt.Y( |
49 | 66 | "price:Q", |
50 | 67 | title="Stock Price ($)", |
51 | 68 | scale=alt.Scale(zero=False), |
52 | | - axis=alt.Axis(labelFontSize=18, titleFontSize=22, grid=True, gridOpacity=0.3), |
| 69 | + axis=alt.Axis( |
| 70 | + labelFontSize=18, |
| 71 | + titleFontSize=22, |
| 72 | + labelColor=INK_SOFT, |
| 73 | + titleColor=INK, |
| 74 | + domainColor=INK_SOFT, |
| 75 | + gridColor=INK, |
| 76 | + gridOpacity=0.10, |
| 77 | + ), |
53 | 78 | ), |
54 | 79 | tooltip=[ |
55 | 80 | alt.Tooltip("date:T", title="Date", format="%B %d, %Y"), |
56 | 81 | alt.Tooltip("price:Q", title="Price", format="$.2f"), |
57 | 82 | ], |
58 | 83 | ) |
59 | 84 | .properties( |
60 | | - width=1600, height=900, title=alt.Title("line-timeseries · altair · pyplots.ai", fontSize=28, anchor="middle") |
| 85 | + width=1600, |
| 86 | + height=900, |
| 87 | + background=PAGE_BG, |
| 88 | + title=alt.Title("line-timeseries · altair · anyplot.ai", fontSize=28, anchor="middle", color=INK), |
61 | 89 | ) |
62 | | - .configure_view(strokeWidth=0) |
63 | | - .configure_axis(gridColor="#cccccc", gridOpacity=0.3, domainColor="#333333") |
| 90 | + .configure_view(fill=PAGE_BG, stroke=INK_SOFT, strokeWidth=1) |
| 91 | + .configure_title(color=INK, fontSize=28) |
| 92 | + .configure_legend(fillColor=ELEVATED_BG, strokeColor=INK_SOFT, labelColor=INK_SOFT, titleColor=INK) |
64 | 93 | .interactive() |
65 | 94 | ) |
66 | 95 |
|
67 | 96 | # Save outputs |
68 | | -chart.save("plot.png", scale_factor=3.0) |
69 | | -chart.save("plot.html") |
| 97 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 98 | +chart.save(os.path.join(script_dir, f"plot-{THEME}.png"), scale_factor=3.0) |
| 99 | +chart.save(os.path.join(script_dir, f"plot-{THEME}.html")) |
0 commit comments