Wednesday, September 28, 2016

Python: Matplotlib: Streamplot (2D Vector Field) Example

Noteworthy:

numpy.mgrid
numpy.sqrt
matplotlib.pyplot.subplots()
matplotlib.pyplot.cm.autumn
streamplot()
strm.linesspeed.max()


Command:

$ cat Downloads/streamplot_demo_features.py


Result:

"""
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:

    * Varying the color along a streamline.
    * Varying the density of streamlines.
    * Varying the line width along a stream line.
"""
import numpy as np
import matplotlib.pyplot as plt

plt.style.use('dark_background')

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

fig0, ax0 = plt.subplots()
strm = ax0.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
fig0.colorbar(strm.lines)

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

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

plt.show()


Command:

$ python Downloads/streamplot_demo_features.py


Graphical output:

fig0

fig1 (w/ unknown error on right side)
fig1 (Normal background style)