Skip to content

Commit a2f8a8b

Browse files
committed
Add zoomed in region to show smoothness of streamlines
1 parent c556ff7 commit a2f8a8b

File tree

3 files changed

+77
-42
lines changed

3 files changed

+77
-42
lines changed

galleries/examples/images_contours_and_fields/plot_streamplot.py

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
axs[7].set_title('Streamplot with unbroken streamlines')
7878

7979
plt.tight_layout()
80-
plt.show()
80+
# plt.show()
8181

8282
# %%
8383
# Streamline computation
@@ -93,7 +93,7 @@
9393
# This example shows how adjusting the maximum allowed step size and error for
9494
# the integrator changes the appearance of the streamline. The differences can
9595
# be subtle, but can be observed particularly where the streamlines have
96-
# high curvature.
96+
# high curvature (as shown in the zoomed in region).
9797

9898
# Linear potential flow over a lifting cylinder
9999
n = 50
@@ -110,37 +110,56 @@
110110
seed_pts = np.column_stack((np.full(n_seed, -1.75), np.linspace(-2, 2, n_seed)))
111111

112112
_, axs = plt.subplots(3, 1, figsize=(6, 14))
113+
th_circ = np.linspace(0, 2 * np.pi, 100)
113114
for ax, max_val in zip(axs, [0.05, 1, 5]):
114-
t_start = time.time()
115-
ax.streamplot(
116-
x,
117-
y,
118-
vx,
119-
vy,
120-
start_points=seed_pts,
121-
broken_streamlines=False,
122-
arrowsize=1e-10,
123-
linewidth=0.6,
124-
color="k",
125-
integration_max_step_scale=max_val,
126-
integration_max_error_scale=max_val,
127-
)
128-
115+
ax_ins = ax.inset_axes([0.0, 0.7, 0.3, 0.35])
116+
for ax_curr, is_inset in zip([ax, ax_ins], [False, True]):
117+
t_start = time.time()
118+
ax_curr.streamplot(
119+
x,
120+
y,
121+
vx,
122+
vy,
123+
start_points=seed_pts,
124+
broken_streamlines=False,
125+
arrowsize=1e-10,
126+
linewidth=2 if is_inset else 0.6,
127+
color="k",
128+
integration_max_step_scale=max_val,
129+
integration_max_error_scale=max_val,
130+
)
131+
if is_inset:
132+
t_total = time.time() - t_start
133+
134+
# Draw the cylinder
135+
ax_curr.fill(
136+
np.cos(th_circ),
137+
np.sin(th_circ),
138+
color="w",
139+
ec="k",
140+
lw=6 if is_inset else 2,
141+
)
142+
143+
# Set axis properties
144+
ax_curr.set_aspect("equal")
145+
146+
# Label properties of each circle
129147
text = f"integration_max_step_scale: {max_val}\n" \
130148
f"integration_max_error_scale: {max_val}\n" \
131-
f"streamplot time: {time.time() - t_start:.2f} sec"
149+
f"streamplot time: {t_total:.2f} sec"
132150
if max_val == 1:
133151
text += "\n(default)"
134152
ax.text(0.0, 0.0, text, ha="center", va="center")
135153

136-
# Draw the cylinder
137-
th_circ = np.linspace(0, 2 * np.pi, 100)
138-
for ax in axs:
139-
ax.fill(np.cos(th_circ), np.sin(th_circ), color="w", ec="k")
154+
# Set axis limits and show zoomed region
155+
ax_ins.set_xlim(-1.2, -0.7)
156+
ax_ins.set_ylim(-0.8, -0.4)
157+
ax_ins.set_yticks(())
158+
ax_ins.set_xticks(())
140159

141-
ax.set_aspect("equal")
142160
ax.set_ylim(-1.5, 1.5)
143161
ax.axis("off")
162+
ax.indicate_inset_zoom(ax_ins, ec="k")
144163

145164
plt.tight_layout()
146165
plt.show()
Loading

lib/matplotlib/tests/test_streamplot.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,45 @@ def test_integration_options():
117117
seed_pts = np.column_stack((np.full(n_seed, -1.75), np.linspace(-2, 2, n_seed)))
118118

119119
fig, axs = plt.subplots(3, 1, figsize=(6, 14))
120-
for ax, max_val in zip(axs, [0.05, 1, 5]):
121-
ax.streamplot(
122-
x,
123-
y,
124-
vx,
125-
vy,
126-
start_points=seed_pts,
127-
broken_streamlines=False,
128-
arrowsize=1e-10,
129-
linewidth=0.6,
130-
color="k",
131-
integration_max_step_scale=max_val,
132-
integration_max_error_scale=max_val,
133-
)
134-
135-
# Draw the cylinder
136120
th_circ = np.linspace(0, 2 * np.pi, 100)
137-
for ax in axs:
138-
ax.fill(np.cos(th_circ), np.sin(th_circ), color="w", ec="k")
121+
for ax, max_val in zip(axs, [0.05, 1, 5]):
122+
ax_ins = ax.inset_axes([0.0, 0.7, 0.3, 0.35])
123+
for ax_curr, is_inset in zip([ax, ax_ins], [False, True]):
124+
ax_curr.streamplot(
125+
x,
126+
y,
127+
vx,
128+
vy,
129+
start_points=seed_pts,
130+
broken_streamlines=False,
131+
arrowsize=1e-10,
132+
linewidth=2 if is_inset else 0.6,
133+
color="k",
134+
integration_max_step_scale=max_val,
135+
integration_max_error_scale=max_val,
136+
)
137+
138+
# Draw the cylinder
139+
ax_curr.fill(
140+
np.cos(th_circ),
141+
np.sin(th_circ),
142+
color="w",
143+
ec="k",
144+
lw=6 if is_inset else 2,
145+
)
146+
147+
# Set axis properties
148+
ax_curr.set_aspect("equal")
149+
150+
# Set axis limits and show zoomed region
151+
ax_ins.set_xlim(-1.2, -0.7)
152+
ax_ins.set_ylim(-0.8, -0.4)
153+
ax_ins.set_yticks(())
154+
ax_ins.set_xticks(())
139155

140-
ax.set_aspect("equal")
141156
ax.set_ylim(-1.5, 1.5)
142157
ax.axis("off")
158+
ax.indicate_inset_zoom(ax_ins, ec="k")
143159

144160
fig.tight_layout()
145161

0 commit comments

Comments
 (0)