Create Streamplot with Matplotlib in Python

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This tutorial will guide you through the process of creating a streamplot using Matplotlib in Python. Streamplot is a 2D vector field that displays a set of streamlines. It is used to visualize fluid flow and other vector fields. In this tutorial, we will show you how to create a streamplot with varying density, color, and line width along with controlling the starting points of streamlines.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import Libraries

Before starting, we need to import the required libraries. In this tutorial, we will be using Numpy and Matplotlib libraries. Numpy is used for numerical operations and Matplotlib is used for data visualization.

import matplotlib.pyplot as plt
import numpy as np

Create Data

We will create the data for our streamplot using the Numpy library. In this example, we will create a meshgrid with 100 points in both directions and calculate the U and V components of our vector field.

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)

Varying Density

In this step, we will create a streamplot with varying density. The density parameter controls the number of streamlines to be plotted. Higher values will result in more streamlines.

plt.streamplot(X, Y, U, V, density=[0.5, 1])
plt.title('Varying Density')
plt.show()

Varying Color

In this step, we will create a streamplot with varying color. The color parameter takes a 2D array that represents the magnitude of the vector field. Here, we are using the U component of the vector field as the color.

strm = plt.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn')
plt.colorbar(strm.lines)
plt.title('Varying Color')
plt.show()

Varying Line Width

In this step, we will create a streamplot with varying line width. The linewidth parameter controls the width of the streamlines. Here, we are using the speed array that we calculated earlier to vary the linewidth.

lw = 5*speed / speed.max()
plt.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)
plt.title('Varying Line Width')
plt.show()

Controlling Starting Points

In this step, we will create a streamplot with controlled starting points. The start_points parameter takes a 2D array that represents the starting points of the streamlines.

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

strm = plt.streamplot(X, Y, U, V, color=U, linewidth=2,
                      cmap='autumn', start_points=seed_points.T)
plt.colorbar(strm.lines)
plt.title('Controlling Starting Points')
plt.plot(seed_points[0], seed_points[1], 'bo')
plt.xlim(-w, w)
plt.ylim(-w, w)
plt.show()

Streamplot with Masking

In this step, we will create a streamplot with masking. We will create a mask and apply it to the U component of our vector field. The masked region will be skipped by the streamlines.

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)

plt.streamplot(X, Y, U, V, color='r')
plt.title('Streamplot with Masking')
plt.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5, cmap='gray', aspect='auto')
plt.gca().set_aspect('equal')
plt.show()

Unbroken Streamlines

In this step, we will create a streamplot with unbroken streamlines. The broken_streamlines parameter controls whether the streamlines should be broken when they exceed the limit of lines within a single grid cell.

plt.streamplot(X, Y, U, V, broken_streamlines=False)
plt.title('Streamplot with Unbroken Streamlines')
plt.show()

Summary

In this tutorial, we learned how to create a streamplot using Matplotlib in Python. We covered various parameters of the streamplot function, including varying density, color, and line width. We also learned how to control the starting points of streamlines, apply masks, and plot unbroken streamlines.

Other Matplotlib Tutorials you may like