|
| 1 | +""" pyplots.ai |
| 2 | +polar-line: Polar Line Plot |
| 3 | +Library: plotly 6.5.0 | Python 3.13.11 |
| 4 | +Quality: 94/100 | Created: 2025-12-30 |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import plotly.graph_objects as go |
| 9 | + |
| 10 | + |
| 11 | +# Data - Average hourly temperature pattern (two seasons) |
| 12 | +np.random.seed(42) |
| 13 | +hours = np.arange(0, 360, 15) # 24 hours mapped to 360 degrees (15 deg = 1 hour) |
| 14 | +hour_labels = [f"{h}:00" for h in range(24)] |
| 15 | + |
| 16 | +# Summer pattern: warmer during day (12-18h), cooler at night |
| 17 | +summer_base = 22 + 8 * np.sin(np.radians(hours - 90)) # Peak at 180 deg (noon) |
| 18 | +summer_temp = summer_base + np.random.normal(0, 0.5, len(hours)) |
| 19 | + |
| 20 | +# Winter pattern: colder overall, less variation |
| 21 | +winter_base = 5 + 5 * np.sin(np.radians(hours - 90)) # Peak at 180 deg (noon) |
| 22 | +winter_temp = winter_base + np.random.normal(0, 0.3, len(hours)) |
| 23 | + |
| 24 | +# Close the loop by appending the first value |
| 25 | +hours_closed = np.append(hours, hours[0]) |
| 26 | +summer_closed = np.append(summer_temp, summer_temp[0]) |
| 27 | +winter_closed = np.append(winter_temp, winter_temp[0]) |
| 28 | + |
| 29 | +# Create figure |
| 30 | +fig = go.Figure() |
| 31 | + |
| 32 | +# Summer line |
| 33 | +fig.add_trace( |
| 34 | + go.Scatterpolar( |
| 35 | + r=summer_closed, |
| 36 | + theta=hours_closed, |
| 37 | + mode="lines+markers", |
| 38 | + name="Summer", |
| 39 | + line=dict(color="#FFD43B", width=4), |
| 40 | + marker=dict(size=10, color="#FFD43B"), |
| 41 | + hovertemplate="Hour: %{theta}°<br>Temp: %{r:.1f}°C<extra>Summer</extra>", |
| 42 | + ) |
| 43 | +) |
| 44 | + |
| 45 | +# Winter line |
| 46 | +fig.add_trace( |
| 47 | + go.Scatterpolar( |
| 48 | + r=winter_closed, |
| 49 | + theta=hours_closed, |
| 50 | + mode="lines+markers", |
| 51 | + name="Winter", |
| 52 | + line=dict(color="#306998", width=4), |
| 53 | + marker=dict(size=10, color="#306998"), |
| 54 | + hovertemplate="Hour: %{theta}°<br>Temp: %{r:.1f}°C<extra>Winter</extra>", |
| 55 | + ) |
| 56 | +) |
| 57 | + |
| 58 | +# Layout |
| 59 | +fig.update_layout( |
| 60 | + title=dict( |
| 61 | + text="Hourly Temperature Pattern · polar-line · plotly · pyplots.ai", |
| 62 | + font=dict(size=28), |
| 63 | + x=0.5, |
| 64 | + xanchor="center", |
| 65 | + ), |
| 66 | + polar=dict( |
| 67 | + radialaxis=dict( |
| 68 | + visible=True, |
| 69 | + range=[0, 35], |
| 70 | + tickfont=dict(size=18), |
| 71 | + title=dict(text="Temperature (°C)", font=dict(size=20)), |
| 72 | + gridcolor="rgba(0,0,0,0.2)", |
| 73 | + ticksuffix="°C", |
| 74 | + ), |
| 75 | + angularaxis=dict( |
| 76 | + tickfont=dict(size=16), |
| 77 | + direction="clockwise", |
| 78 | + rotation=90, # Start from top (midnight) |
| 79 | + tickmode="array", |
| 80 | + tickvals=list(range(0, 360, 15)), |
| 81 | + ticktext=hour_labels, |
| 82 | + gridcolor="rgba(0,0,0,0.2)", |
| 83 | + ), |
| 84 | + bgcolor="rgba(255,255,255,0.9)", |
| 85 | + ), |
| 86 | + legend=dict( |
| 87 | + font=dict(size=20), x=1.05, y=0.5, bgcolor="rgba(255,255,255,0.8)", bordercolor="rgba(0,0,0,0.3)", borderwidth=1 |
| 88 | + ), |
| 89 | + template="plotly_white", |
| 90 | + margin=dict(l=80, r=150, t=100, b=80), |
| 91 | +) |
| 92 | + |
| 93 | +# Save outputs |
| 94 | +fig.write_image("plot.png", width=1600, height=900, scale=3) |
| 95 | +fig.write_html("plot.html", include_plotlyjs="cdn") |
0 commit comments