|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | line-basic: Basic Line Plot |
3 | | -Library: altair 6.0.0 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: altair 6.1.0 | Python 3.13.13 |
| 4 | +Quality: 86/100 | Updated: 2026-04-29 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import altair as alt |
8 | 10 | import numpy as np |
9 | 11 | import pandas as pd |
10 | 12 |
|
11 | 13 |
|
| 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 | +BRAND = "#009E73" |
| 21 | + |
12 | 22 | # Data - Monthly temperature readings over a year |
13 | 23 | np.random.seed(42) |
14 | 24 | months = pd.date_range(start="2024-01-01", periods=12, freq="MS") |
15 | | -# Simulate temperature pattern (cold in winter, warm in summer) |
16 | 25 | base_temp = 15 + 12 * np.sin((np.arange(12) - 3) * np.pi / 6) |
17 | 26 | temperature = base_temp + np.random.randn(12) * 2 |
18 | | - |
19 | 27 | df = pd.DataFrame({"Month": months, "Temperature": temperature}) |
20 | 28 |
|
21 | 29 | # Plot |
22 | | -chart = ( |
| 30 | +tooltip_enc = [ |
| 31 | + alt.Tooltip("Month:T", title="Month", format="%B %Y"), |
| 32 | + alt.Tooltip("Temperature:Q", title="Temperature (°C)", format=".1f"), |
| 33 | +] |
| 34 | + |
| 35 | +line = ( |
23 | 36 | alt.Chart(df) |
24 | | - .mark_line(strokeWidth=4, color="#306998") |
25 | | - .encode( |
26 | | - x=alt.X("Month:T", title="Month", axis=alt.Axis(labelFontSize=18, titleFontSize=22)), |
27 | | - y=alt.Y("Temperature:Q", title="Temperature (°C)", axis=alt.Axis(labelFontSize=18, titleFontSize=22)), |
28 | | - ) |
29 | | - .properties(width=1600, height=900, title=alt.Title("line-basic · altair · pyplots.ai", fontSize=28)) |
| 37 | + .mark_line(strokeWidth=4, color=BRAND) |
| 38 | + .encode(x=alt.X("Month:T", title="Month"), y=alt.Y("Temperature:Q", title="Temperature (°C)")) |
30 | 39 | ) |
31 | 40 |
|
32 | | -# Add points to enhance visibility |
33 | | -points = alt.Chart(df).mark_point(size=200, color="#306998", filled=True).encode(x="Month:T", y="Temperature:Q") |
| 41 | +points = ( |
| 42 | + alt.Chart(df) |
| 43 | + .mark_point(size=200, color=BRAND, filled=True) |
| 44 | + .encode(x="Month:T", y="Temperature:Q", tooltip=tooltip_enc) |
| 45 | +) |
34 | 46 |
|
35 | | -# Combine line and points with subtle grid |
36 | | -final_chart = (chart + points).configure_axis(gridColor="#E0E0E0", gridOpacity=0.3).configure_view(strokeWidth=0) |
| 47 | +chart = ( |
| 48 | + (line + points) |
| 49 | + .interactive() |
| 50 | + .properties( |
| 51 | + width=1600, height=900, title=alt.Title("line-basic · altair · anyplot.ai", fontSize=28), background=PAGE_BG |
| 52 | + ) |
| 53 | + .configure_view(fill=PAGE_BG, stroke=INK_SOFT) |
| 54 | + .configure_axis( |
| 55 | + domainColor=INK_SOFT, |
| 56 | + tickColor=INK_SOFT, |
| 57 | + gridColor=INK, |
| 58 | + gridOpacity=0.15, |
| 59 | + labelColor=INK_SOFT, |
| 60 | + titleColor=INK, |
| 61 | + labelFontSize=18, |
| 62 | + titleFontSize=22, |
| 63 | + ) |
| 64 | + .configure_title(color=INK, fontSize=28) |
| 65 | + .configure_legend(fillColor=ELEVATED_BG, strokeColor=INK_SOFT, labelColor=INK_SOFT, titleColor=INK) |
| 66 | +) |
37 | 67 |
|
38 | 68 | # Save |
39 | | -final_chart.save("plot.png", scale_factor=3.0) |
40 | | -final_chart.save("plot.html") |
| 69 | +chart.save(f"plot-{THEME}.png", scale_factor=3.0) |
| 70 | +chart.save(f"plot-{THEME}.html") |
0 commit comments