|
| 1 | +""" pyplots.ai |
| 2 | +streamline-basic: Basic Streamline Plot |
| 3 | +Library: altair 6.0.0 | Python 3.13.11 |
| 4 | +Quality: 91/100 | Created: 2025-12-31 |
| 5 | +""" |
| 6 | + |
| 7 | +import altair as alt |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | + |
| 12 | +# Disable data row limit |
| 13 | +alt.data_transformers.disable_max_rows() |
| 14 | + |
| 15 | +# Data - Create a vector field for a vortex flow (u = -y, v = x) |
| 16 | +np.random.seed(42) |
| 17 | + |
| 18 | +# Generate streamlines using Euler integration - flat KISS structure |
| 19 | +streamlines_data = [] |
| 20 | +streamline_id = 0 |
| 21 | + |
| 22 | +# Starting points at different radii for vortex visualization |
| 23 | +radii = [0.4, 0.7, 1.0, 1.4, 1.8, 2.2, 2.6, 3.0] |
| 24 | +n_per_radius = 6 |
| 25 | +dt = 0.03 |
| 26 | +max_steps = 250 |
| 27 | + |
| 28 | +for r in radii: |
| 29 | + for i in range(n_per_radius): |
| 30 | + angle = 2 * np.pi * i / n_per_radius + (r * 0.1) |
| 31 | + x = r * np.cos(angle) |
| 32 | + y = r * np.sin(angle) |
| 33 | + points = [(x, y)] |
| 34 | + |
| 35 | + # Trace streamline using Euler integration |
| 36 | + for _ in range(max_steps): |
| 37 | + # Vector field: circular vortex (u = -y, v = x) |
| 38 | + u = -y |
| 39 | + v = x |
| 40 | + mag = np.sqrt(u**2 + v**2) |
| 41 | + if mag < 1e-6: |
| 42 | + break |
| 43 | + # Normalize and step |
| 44 | + x_new = x + dt * u / mag |
| 45 | + y_new = y + dt * v / mag |
| 46 | + # Stop if out of bounds |
| 47 | + if abs(x_new) > 3.2 or abs(y_new) > 3.2: |
| 48 | + break |
| 49 | + x, y = x_new, y_new |
| 50 | + points.append((x, y)) |
| 51 | + |
| 52 | + # Only include streamlines with enough points |
| 53 | + if len(points) > 5: |
| 54 | + for j, (px, py) in enumerate(points): |
| 55 | + # Velocity magnitude equals distance from center in this vortex |
| 56 | + vel = np.sqrt(px**2 + py**2) |
| 57 | + streamlines_data.append( |
| 58 | + {"x": float(px), "y": float(py), "streamline_id": streamline_id, "order": j, "velocity": float(vel)} |
| 59 | + ) |
| 60 | + streamline_id += 1 |
| 61 | + |
| 62 | +df = pd.DataFrame(streamlines_data) |
| 63 | + |
| 64 | +# Compute average velocity per streamline for color encoding |
| 65 | +avg_velocity = df.groupby("streamline_id")["velocity"].mean().reset_index() |
| 66 | +avg_velocity.columns = ["streamline_id", "avg_velocity"] |
| 67 | +df = df.merge(avg_velocity, on="streamline_id") |
| 68 | + |
| 69 | +# Create the streamline chart using line marks |
| 70 | +# Color by average velocity (flow speed) for each streamline |
| 71 | +chart = ( |
| 72 | + alt.Chart(df) |
| 73 | + .mark_line(strokeWidth=2.5, opacity=0.85) |
| 74 | + .encode( |
| 75 | + x=alt.X("x:Q", title="X Position (units)", scale=alt.Scale(domain=[-3.5, 3.5])), |
| 76 | + y=alt.Y("y:Q", title="Y Position (units)", scale=alt.Scale(domain=[-3.5, 3.5])), |
| 77 | + color=alt.Color( |
| 78 | + "avg_velocity:Q", |
| 79 | + scale=alt.Scale(scheme="viridis"), |
| 80 | + title="Flow Speed", |
| 81 | + legend=alt.Legend(titleFontSize=18, labelFontSize=16, gradientLength=200), |
| 82 | + ), |
| 83 | + detail="streamline_id:N", |
| 84 | + order="order:O", |
| 85 | + ) |
| 86 | + .properties( |
| 87 | + width=1600, height=900, title=alt.Title("streamline-basic · altair · pyplots.ai", fontSize=28, anchor="middle") |
| 88 | + ) |
| 89 | + .configure_axis(labelFontSize=18, titleFontSize=22, grid=True, gridColor="#cccccc", gridOpacity=0.4) |
| 90 | + .configure_view(strokeWidth=0) |
| 91 | +) |
| 92 | + |
| 93 | +# Save as PNG and HTML |
| 94 | +chart.save("plot.png", scale_factor=3.0) |
| 95 | +chart.save("plot.html") |
0 commit comments