
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/images_contours_and_fields/plot_streamplot.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_images_contours_and_fields_plot_streamplot.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_images_contours_and_fields_plot_streamplot.py:


==========
Streamplot
==========

A stream plot, or streamline plot, is used to display 2D vector fields. This
example shows a few features of the `~.axes.Axes.streamplot` function:

* Varying the color along a streamline.
* Varying the density of streamlines.
* Varying the line width along a streamline.
* Controlling the starting points of streamlines.
* Streamlines skipping masked regions and NaN values.
* Unbroken streamlines even when exceeding the limit of lines within a single
  grid cell.

.. GENERATED FROM PYTHON SOURCE LINES 17-73

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    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**2 + V**2)

    fig, axs = plt.subplots(3, 2, figsize=(7, 9), height_ratios=[1, 1, 2])
    axs = axs.flat

    #  Varying density along a streamline
    axs[0].streamplot(X, Y, U, V, density=[0.5, 1])
    axs[0].set_title('Varying Density')

    # Varying color along a streamline
    strm = axs[1].streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn')
    fig.colorbar(strm.lines)
    axs[1].set_title('Varying Color')

    #  Varying line width along a streamline
    lw = 5*speed / speed.max()
    axs[2].streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)
    axs[2].set_title('Varying Line Width')

    # Controlling the starting points of the streamlines
    seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1,  0, 1, 2, 2]])

    strm = axs[3].streamplot(X, Y, U, V, color=U, linewidth=2,
                             cmap='autumn', start_points=seed_points.T)
    fig.colorbar(strm.lines)
    axs[3].set_title('Controlling Starting Points')

    # Displaying the starting points with blue symbols.
    axs[3].plot(seed_points[0], seed_points[1], 'bo')
    axs[3].set(xlim=(-w, w), ylim=(-w, w))

    # Create a mask
    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)

    axs[4].streamplot(X, Y, U, V, color='r')
    axs[4].set_title('Streamplot with Masking')

    axs[4].imshow(~mask, extent=(-w, w, -w, w), alpha=0.5, cmap='gray',
                  aspect='auto')
    axs[4].set_aspect('equal')

    axs[5].streamplot(X, Y, U, V, broken_streamlines=False)
    axs[5].set_title('Streamplot with unbroken streamlines')

    plt.tight_layout()
    plt.show()



.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_plot_streamplot_001.png
   :alt: Varying Density, Varying Color, Varying Line Width, Controlling Starting Points, Streamplot with Masking, Streamplot with unbroken streamlines
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_plot_streamplot_001.png, /gallery/images_contours_and_fields/images/sphx_glr_plot_streamplot_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 74-81

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.axes.Axes.streamplot` / `matplotlib.pyplot.streamplot`
   - `matplotlib.gridspec.GridSpec`


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 3.035 seconds)


.. _sphx_glr_download_gallery_images_contours_and_fields_plot_streamplot.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_streamplot.ipynb <plot_streamplot.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_streamplot.py <plot_streamplot.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_streamplot.zip <plot_streamplot.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
