|
| 1 | +""" pyplots.ai |
| 2 | +polar-bar: Polar Bar Chart (Wind Rose) |
| 3 | +Library: altair 6.0.0 | Python 3.13.11 |
| 4 | +Quality: 91/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 - Wind direction frequency data |
| 13 | +np.random.seed(42) |
| 14 | +directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"] |
| 15 | +angles = [0, 45, 90, 135, 180, 225, 270, 315] # degrees from north |
| 16 | + |
| 17 | +# Simulate realistic wind frequency with prevailing westerlies |
| 18 | +base_freq = [12, 8, 10, 6, 9, 18, 22, 16] # Higher for W, SW, NW |
| 19 | +frequencies = [f + np.random.randint(-2, 3) for f in base_freq] |
| 20 | + |
| 21 | +# Create DataFrame with angle in radians for Altair |
| 22 | +df = pd.DataFrame({"direction": directions, "angle": angles, "frequency": frequencies}) |
| 23 | + |
| 24 | +# Calculate start and end angles for each bar (in radians) |
| 25 | +# Altair uses radians, and we need to center each bar on its direction |
| 26 | +bar_width = 40 # degrees |
| 27 | +df["theta_start"] = np.radians(df["angle"] - bar_width / 2) |
| 28 | +df["theta_end"] = np.radians(df["angle"] + bar_width / 2) |
| 29 | + |
| 30 | +# Create the polar bar chart using mark_arc |
| 31 | +base = alt.Chart(df) |
| 32 | + |
| 33 | +# Bars radiating from center using arc marks |
| 34 | +bars = base.mark_arc(stroke="white", strokeWidth=2).encode( |
| 35 | + theta=alt.Theta("theta_start:Q", scale=alt.Scale(domain=[0, 2 * np.pi])), |
| 36 | + theta2="theta_end:Q", |
| 37 | + radius=alt.Radius("frequency:Q", scale=alt.Scale(type="linear", zero=True, rangeMin=0)), |
| 38 | + color=alt.Color( |
| 39 | + "frequency:Q", |
| 40 | + scale=alt.Scale(scheme="blues"), |
| 41 | + legend=alt.Legend(title="Frequency (days)", titleFontSize=18, labelFontSize=16, orient="right"), |
| 42 | + ), |
| 43 | + tooltip=[alt.Tooltip("direction:N", title="Direction"), alt.Tooltip("frequency:Q", title="Frequency (days)")], |
| 44 | +) |
| 45 | + |
| 46 | +# Add direction labels around the perimeter |
| 47 | +max_freq = max(frequencies) * 1.25 |
| 48 | +label_df = pd.DataFrame({"direction": directions, "angle_rad": np.radians(angles), "radius": [max_freq] * 8}) |
| 49 | + |
| 50 | +# Calculate x, y positions for labels |
| 51 | +label_df["x"] = label_df["radius"] * np.sin(label_df["angle_rad"]) |
| 52 | +label_df["y"] = label_df["radius"] * np.cos(label_df["angle_rad"]) |
| 53 | + |
| 54 | +labels = ( |
| 55 | + alt.Chart(label_df) |
| 56 | + .mark_text(fontSize=20, fontWeight="bold", color="#306998") |
| 57 | + .encode(x=alt.X("x:Q", axis=None), y=alt.Y("y:Q", axis=None), text="direction:N") |
| 58 | +) |
| 59 | + |
| 60 | +# Combine bars and labels |
| 61 | +chart = ( |
| 62 | + alt.layer(bars, labels) |
| 63 | + .properties( |
| 64 | + width=900, |
| 65 | + height=900, |
| 66 | + title=alt.Title(text="polar-bar · altair · pyplots.ai", fontSize=28, anchor="middle", color="#333333"), |
| 67 | + ) |
| 68 | + .configure_view(strokeWidth=0) |
| 69 | + .interactive() |
| 70 | +) |
| 71 | + |
| 72 | +# Save outputs |
| 73 | +chart.save("plot.png", scale_factor=4.5) |
| 74 | +chart.save("plot.html") |
0 commit comments