|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | line-timeseries: Time Series Line Plot |
3 | | -Library: pygal 3.1.0 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-26 |
| 3 | +Library: pygal 3.1.0 | Python 3.13.13 |
| 4 | +Quality: 85/100 | Updated: 2026-05-09 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
7 | 8 | import random |
| 9 | +import sys |
8 | 10 | from datetime import datetime, timedelta |
9 | 11 |
|
10 | | -import pygal |
11 | | -from pygal.style import Style |
12 | 12 |
|
| 13 | +# Ensure site-packages is in path before current directory to avoid shadowing |
| 14 | +site_packages = next((p for p in sys.path if "site-packages" in p), None) |
| 15 | +if site_packages and sys.path[0] == os.path.dirname(__file__): |
| 16 | + sys.path.remove(sys.path[0]) |
| 17 | + sys.path.insert(0, site_packages) |
| 18 | + |
| 19 | +import pygal # noqa: E402 |
| 20 | +from pygal.style import Style # noqa: E402 |
| 21 | + |
| 22 | + |
| 23 | +# Theme tokens |
| 24 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 25 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 26 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 27 | +INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F" |
| 28 | + |
| 29 | +# Okabe-Ito palette |
| 30 | +OKABE_ITO = ("#009E73", "#D55E00", "#0072B2", "#CC79A7", "#E69F00", "#56B4E9", "#F0E442") |
13 | 31 |
|
14 | 32 | # Seed for reproducibility |
15 | 33 | random.seed(42) |
|
22 | 40 | price = 150.0 |
23 | 41 | prices = [] |
24 | 42 | for _ in range(365): |
25 | | - # Add slight upward trend with random daily changes |
26 | 43 | change = random.gauss(0.1, 2.5) |
27 | | - price = max(100, price + change) # Prevent going too low |
| 44 | + price = max(100, price + change) |
28 | 45 | prices.append(round(price, 2)) |
29 | 46 |
|
30 | | -# Custom style for 4800x2700 canvas with larger fonts |
| 47 | +# Custom style for 4800x2700 canvas |
31 | 48 | custom_style = Style( |
32 | | - background="white", |
33 | | - plot_background="white", |
34 | | - foreground="#333333", |
35 | | - foreground_strong="#333333", |
36 | | - foreground_subtle="#666666", |
37 | | - colors=("#306998",), # Python Blue |
38 | | - title_font_size=72, |
39 | | - label_font_size=48, |
40 | | - major_label_font_size=42, |
41 | | - legend_font_size=42, |
42 | | - value_font_size=36, |
43 | | - guide_stroke_color="#cccccc", |
44 | | - guide_stroke_dasharray="2,4", |
| 49 | + background=PAGE_BG, |
| 50 | + plot_background=PAGE_BG, |
| 51 | + foreground=INK, |
| 52 | + foreground_strong=INK, |
| 53 | + foreground_subtle=INK_MUTED, |
| 54 | + colors=OKABE_ITO, |
| 55 | + title_font_size=28, |
| 56 | + label_font_size=22, |
| 57 | + major_label_font_size=18, |
| 58 | + legend_font_size=16, |
| 59 | + value_font_size=14, |
| 60 | + stroke_width=6, |
45 | 61 | ) |
46 | 62 |
|
47 | 63 | # Create line chart |
48 | 64 | chart = pygal.Line( |
49 | 65 | width=4800, |
50 | 66 | height=2700, |
51 | 67 | style=custom_style, |
52 | | - title="line-timeseries · pygal · pyplots.ai", |
| 68 | + title="line-timeseries · pygal · anyplot.ai", |
53 | 69 | x_title="Date", |
54 | 70 | y_title="Stock Price (USD)", |
55 | | - show_x_guides=False, |
| 71 | + show_x_guides=True, |
56 | 72 | show_y_guides=True, |
57 | 73 | x_label_rotation=45, |
58 | 74 | show_legend=True, |
59 | 75 | legend_at_bottom=True, |
60 | 76 | truncate_legend=-1, |
61 | 77 | show_dots=False, |
62 | | - stroke_style={"width": 5}, |
63 | | - margin=60, |
| 78 | + margin=100, |
64 | 79 | ) |
65 | 80 |
|
66 | 81 | # Add data series |
|
70 | 85 | x_labels = [] |
71 | 86 | x_labels_major = [] |
72 | 87 | for d in dates: |
73 | | - if d.day == 1: # First of each month |
| 88 | + if d.day == 1: |
74 | 89 | x_labels.append(d.strftime("%b %Y")) |
75 | 90 | x_labels_major.append(d.strftime("%b %Y")) |
76 | 91 | else: |
|
80 | 95 | chart.x_labels_major = x_labels_major |
81 | 96 |
|
82 | 97 | # Save as PNG and HTML |
83 | | -chart.render_to_file("plot.html") |
84 | | -chart.render_to_png("plot.png") |
| 98 | +chart.render_to_file(f"plot-{THEME}.html") |
| 99 | +chart.render_to_png(f"plot-{THEME}.png") |
0 commit comments