|
| 1 | +""" pyplots.ai |
| 2 | +line-styled: Styled Line Plot |
| 3 | +Library: altair 6.0.0 | Python 3.13.11 |
| 4 | +Quality: 93/100 | Created: 2025-12-30 |
| 5 | +""" |
| 6 | + |
| 7 | +import altair as alt |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | + |
| 12 | +# Data - Monthly temperature readings from different weather stations |
| 13 | +np.random.seed(42) |
| 14 | +months = np.arange(1, 13) |
| 15 | +month_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] |
| 16 | + |
| 17 | +# Temperature data for 4 weather stations with seasonal patterns |
| 18 | +base_temp = np.array([5, 7, 12, 16, 21, 25, 28, 27, 23, 17, 11, 6]) |
| 19 | +station_a = base_temp + np.random.randn(12) * 1.5 |
| 20 | +station_b = base_temp - 3 + np.random.randn(12) * 1.5 |
| 21 | +station_c = base_temp + 5 + np.random.randn(12) * 1.5 |
| 22 | +station_d = base_temp - 6 + np.random.randn(12) * 1.5 |
| 23 | + |
| 24 | +# Create long-form DataFrame for Altair |
| 25 | +df = pd.DataFrame( |
| 26 | + { |
| 27 | + "Month": month_names * 4, |
| 28 | + "MonthNum": list(months) * 4, |
| 29 | + "Temperature": np.concatenate([station_a, station_b, station_c, station_d]), |
| 30 | + "Station": (["Coastal"] * 12 + ["Mountain"] * 12 + ["Valley"] * 12 + ["Highland"] * 12), |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +# Define line styles (strokeDash) for each station |
| 35 | +line_styles = { |
| 36 | + "Coastal": [0], # solid |
| 37 | + "Mountain": [8, 4], # dashed |
| 38 | + "Valley": [2, 2], # dotted |
| 39 | + "Highland": [8, 4, 2, 4], # dash-dot |
| 40 | +} |
| 41 | + |
| 42 | +# Colors - using Python palette and accessible colors |
| 43 | +station_colors = { |
| 44 | + "Coastal": "#306998", # Python Blue |
| 45 | + "Mountain": "#FFD43B", # Python Yellow |
| 46 | + "Valley": "#48A9A6", # Teal |
| 47 | + "Highland": "#E76F51", # Coral |
| 48 | +} |
| 49 | + |
| 50 | +# Create the chart |
| 51 | +chart = ( |
| 52 | + alt.Chart(df) |
| 53 | + .mark_line( |
| 54 | + strokeWidth=4 # Thicker lines for visibility |
| 55 | + ) |
| 56 | + .encode( |
| 57 | + x=alt.X( |
| 58 | + "MonthNum:O", |
| 59 | + title="Month", |
| 60 | + axis=alt.Axis( |
| 61 | + labelFontSize=18, |
| 62 | + titleFontSize=22, |
| 63 | + values=list(months), |
| 64 | + labelExpr="['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][datum.value - 1]", |
| 65 | + ), |
| 66 | + ), |
| 67 | + y=alt.Y( |
| 68 | + "Temperature:Q", |
| 69 | + title="Temperature (\u00b0C)", |
| 70 | + scale=alt.Scale(domain=[-5, 40]), |
| 71 | + axis=alt.Axis(labelFontSize=18, titleFontSize=22), |
| 72 | + ), |
| 73 | + color=alt.Color( |
| 74 | + "Station:N", |
| 75 | + scale=alt.Scale(domain=list(station_colors.keys()), range=list(station_colors.values())), |
| 76 | + legend=alt.Legend( |
| 77 | + title="Weather Station", titleFontSize=20, labelFontSize=18, symbolStrokeWidth=4, symbolSize=300 |
| 78 | + ), |
| 79 | + ), |
| 80 | + strokeDash=alt.StrokeDash( |
| 81 | + "Station:N", scale=alt.Scale(domain=list(line_styles.keys()), range=list(line_styles.values())), legend=None |
| 82 | + ), # Combine with color legend |
| 83 | + ) |
| 84 | + .properties( |
| 85 | + width=1600, |
| 86 | + height=900, |
| 87 | + title=alt.Title(text="line-styled \u00b7 altair \u00b7 pyplots.ai", fontSize=28, anchor="middle"), |
| 88 | + ) |
| 89 | + .configure_axis(grid=True, gridOpacity=0.3, gridDash=[2, 2]) |
| 90 | + .configure_view(strokeWidth=0) |
| 91 | +) |
| 92 | + |
| 93 | +# Save as PNG (scale_factor=3 gives 4800x2700) |
| 94 | +chart.save("plot.png", scale_factor=3.0) |
| 95 | + |
| 96 | +# Save as HTML for interactivity |
| 97 | +chart.save("plot.html") |
0 commit comments