Skip to content

Merged and improved the streamplot demonstration #8082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/users/prev_whats_new/whats_new_1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ In addition to simply plotting the streamlines of the vector field,
line widths of the streamlines to a separate parameter, such as the speed or
local intensity of the vector field.

.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo_features.py
.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo.py


New hist functionality
Expand Down
2 changes: 1 addition & 1 deletion doc/users/screenshots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ a vector field. In addition to simply plotting the streamlines, it allows you
to map the colors and/or line widths of streamlines to a separate parameter,
such as the speed or local intensity of the vector field.

.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo_features.py
.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo.py

This feature complements the :meth:`~matplotlib.pyplot.quiver` function for
plotting vector fields. Thanks to Tom Flannaghan and Tony Yu for adding the
Expand Down
68 changes: 68 additions & 0 deletions examples/images_contours_and_fields/streamplot_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
==========
Streamplot
==========

Demo of the `streamplot` function.

A streamplot, or streamline plot, is used to display 2D vector fields. This
example shows a few features of the stream plot function:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stream plot <- streamplot?


* Varying the color along a streamline.
* Varying the density of streamlines.
* Varying the line width along a stream line.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stream line <- streamline?

* Streamlines skipping masked regions and NaN values.
* Controlling the start points of streamlines.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the similar comment below (in the relevant code block).

"""
import numpy as np
import matplotlib.pyplot as plt

w = 3
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

# Varying color along a streamline
fig0, ax = plt.subplots()
strm = ax.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn')
fig0.colorbar(strm.lines)

# Varying the density of streamlines
fig1, (ax1, ax2) = plt.subplots(ncols=2)
ax1.streamplot(X, Y, U, V, density=[0.5, 1])

# Varying the line width along a stream line
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would merge this block with the previous one. I find it a bit perturbing that the previous block creates two subplots but only uses one. IMO, the two description comments may then be merged into something like:

# Varying the density of streamlines (ax1) and the line width along
# a streamline (ax2).

lw = 5*speed / speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)

# Streamlines skipping masked regions and NaN values
mask = np.zeros(U.shape, dtype=bool)
mask[40:60, 40:60] = True
U[:20, :20] = np.nan
U = np.ma.array(U, mask=mask)

fig2, ax = plt.subplots()
ax.streamplot(X, Y, U, V, color='r')

ax.imshow(-mask, extent=(-w, w, -w, w), alpha=0.5,
interpolation='nearest', cmap=plt.cm.gray)

# Controlling the start points of streamlines
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Controlling the start points of streamlines <- # Controlling the start points of the streamlines. Disclaimer: I am not a native English speaker, so please forget this comment if I am obviously wrong ;)…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The former refers to any streamlines, while the latter is about these streamlines. More importantly, I think it should be "starting" instead of "start".

X, Y = (np.linspace(-3, 3, 100),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not reuse the previous X and Y arrays? Especially if the purpose is to create similar arrays.

np.linspace(-3, 3, 100))

U, V = np.mgrid[-3:3:100j, 0:0:100j]

seed_points = np.array([[-2, 0, 1], [-2, 0, 1]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for changing the U, V and seed_points arrays compared to the former streamplot_demo_start_points.py script?


fig3, ax = plt.subplots()
strm = ax.streamplot(X, Y, U, V, color=U, linewidth=2,
cmap="autumn", start_points=seed_points.T)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being there, I would replace the double quotes with single ones (i.e. "autumn" <- 'autumn'), to be consistent with the rest of the script.

fig3.colorbar(strm.lines)

ax.plot(seed_points[0], seed_points[1], 'bo')
Copy link
Contributor

@afvincent afvincent Feb 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to add a small comment to precise that this is some kind of an helper plot. Maybe simply:

# Displaying the starting points with red symbols.

Edit: start <- starting (see @QuLogic's comment above)


ax.axis((-3, 3, -3, 3))

plt.show()
33 changes: 0 additions & 33 deletions examples/images_contours_and_fields/streamplot_demo_features.py

This file was deleted.

29 changes: 0 additions & 29 deletions examples/images_contours_and_fields/streamplot_demo_masking.py

This file was deleted.

This file was deleted.