{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Streamplot\n\nA stream plot, or streamline plot, is used to display 2D vector fields. This\nexample shows a few features of the `~.axes.Axes.streamplot` function:\n\n* Varying the color along a streamline.\n* Varying the density of streamlines.\n* Varying the line width along a streamline.\n* Controlling the starting points of streamlines.\n* Streamlines skipping masked regions and NaN values.\n* Unbroken streamlines even when exceeding the limit of lines within a single\n grid cell.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nw = 3\nY, X = np.mgrid[-w:w:100j, -w:w:100j]\nU = -1 - X**2 + Y\nV = 1 + X - Y**2\nspeed = np.sqrt(U**2 + V**2)\n\nfig, axs = plt.subplots(3, 2, figsize=(7, 9), height_ratios=[1, 1, 2])\naxs = axs.flat\n\n# Varying density along a streamline\naxs[0].streamplot(X, Y, U, V, density=[0.5, 1])\naxs[0].set_title('Varying Density')\n\n# Varying color along a streamline\nstrm = axs[1].streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn')\nfig.colorbar(strm.lines)\naxs[1].set_title('Varying Color')\n\n# Varying line width along a streamline\nlw = 5*speed / speed.max()\naxs[2].streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)\naxs[2].set_title('Varying Line Width')\n\n# Controlling the starting points of the streamlines\nseed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]])\n\nstrm = axs[3].streamplot(X, Y, U, V, color=U, linewidth=2,\n cmap='autumn', start_points=seed_points.T)\nfig.colorbar(strm.lines)\naxs[3].set_title('Controlling Starting Points')\n\n# Displaying the starting points with blue symbols.\naxs[3].plot(seed_points[0], seed_points[1], 'bo')\naxs[3].set(xlim=(-w, w), ylim=(-w, w))\n\n# Create a mask\nmask = np.zeros(U.shape, dtype=bool)\nmask[40:60, 40:60] = True\nU[:20, :20] = np.nan\nU = np.ma.array(U, mask=mask)\n\naxs[4].streamplot(X, Y, U, V, color='r')\naxs[4].set_title('Streamplot with Masking')\n\naxs[4].imshow(~mask, extent=(-w, w, -w, w), alpha=0.5, cmap='gray',\n aspect='auto')\naxs[4].set_aspect('equal')\n\naxs[5].streamplot(X, Y, U, V, broken_streamlines=False)\naxs[5].set_title('Streamplot with unbroken streamlines')\n\nplt.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.streamplot` / `matplotlib.pyplot.streamplot`\n - `matplotlib.gridspec.GridSpec`\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }