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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/SpecializedPlotsGroup(["`Specialized Plots`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("`Adding Titles and Labels`") matplotlib/SpecializedPlotsGroup -.-> matplotlib/stream_plots("`Stream Plots`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/with_statement -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} matplotlib/importing_matplotlib -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} matplotlib/figures_axes -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} matplotlib/line_plots -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} matplotlib/heatmaps -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} matplotlib/titles_labels -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} matplotlib/stream_plots -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} python/variables_data_types -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} python/booleans -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} python/lists -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} python/tuples -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} python/importing_modules -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} python/numerical_computing -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} python/data_visualization -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} python/build_in_functions -.-> lab-48870{{"`Create Streamplot with Matplotlib in Python`"}} end

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