Matplotlib - Stream Plot



A stream plot is used to visualize the flow patterns of a fluid, such as air or water. It represents the movement of particles within the fluid over a particular area.

Imagine you have a river, and you want to see how the water is flowing at different points. A stream plot will use lines or curves to show the paths that imaginary particles will follow as they move with the flow of the river. The lines are drawn in a way that they are tangent to the velocity vector at any given point, i.e. they point in the direction the fluid is moving −

Stream Plot

Stream Plot in Matplotlib

We can create a stream plot in Matplotlib using the streamplot() function. This function generates a stream plot by integrating a given vector field, creating streamlines that shows the paths of particles or fluid elements.

The streamplot() Function

The streamplot() function in creates a 2D streamplot to visualize vector fields. It takes grid coordinates (X, Y) and vector components (u, v) as input. Optional parameters control density, line width, color, and other plot characteristics. It provides a visual representation of the flow or direction of the vector field.

Following is the syntax of the streamplot() function in Matplotlib −

matplotlib.pyplot.streamplot(x, y, u, v, density=1, norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.1, **kwargs)

Where,

  • x and y are the coordinates of the grid, where x and y can be 1D or 2D arrays.
  • u and v are the components of the vector field, representing the velocity in the x and y directions, respectively.
  • density controls the number of streamlines in the plot.
  • norm is the normalize instance for mapping the data values to colors.
  • arrowsize is the size of arrowheads at the end of the streamlines.
  • arrowstyle is the style of arrowheads.
  • minlength is the minimum length of streamlines.
  • **kwargs is the additional keyword arguments that can be used for customization.

Basic Stream Plot

A basic stream plot in Matplotlib is a visualization that shows the flow of a vector field using streamlines. Streamlines are curves that follow the direction of the velocity vector at each point, providing a visual representation of how particles or fluids would move.

Example

In the following example, we are creating a grid of points in a 2D space and defining a vector field representing a rotational flow pattern, where particles would follow circular paths. The streamplot() function is then used to create a basic stream plot, visualizing the flow pattern with streamlines −

import matplotlib.pyplot as plt
import numpy as np

# Generating grid of points
x, y = np.meshgrid(np.linspace(-2, 2, 20), np.linspace(-2, 2, 20))

# Defining vector field (rotational field)
u = -y
v = x

# Creating a basic stream plot
plt.streamplot(x, y, u, v, density=1.5, linewidth=1, arrowsize=1.5)

plt.title('Basic Stream Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

Output

After executing the above code, we get the following output −

Basic Stream Plot

Multiple Stream Plots

A multiple stream plots in Matplotlib involves creating more than one stream plot in a single visualization. Each stream plot represents the flow or movement of a vector field using streamlines, and combining them allows for the comparison of multiple vector fields or the same vector field under different conditions.

Example

In here, we are creating a grid of points and defining vector fields for two sources "U1, V1" and "U2, V2". We then use the streamplot() function to generate two stream plots in red and blue color, each representing a vector field with streamlines −

import numpy as np
import matplotlib.pyplot as plt

# Creating a grid
x = np.linspace(-2, 2, 20)
y = np.linspace(-2, 2, 20)
X, Y = np.meshgrid(x, y)

# Defining vector fields for two sources
U1, V1 = X, Y
U2, V2 = -X, -Y

# Plotting multiple stream plots
plt.streamplot(X, Y, U1, V1, density=1, linewidth=1, color='red')
plt.streamplot(X, Y, U2, V2, density=1, linewidth=1, color='blue')
plt.title('Multiple Stream Plots')
plt.show()

Output

Following is the output of the above code −

Multiple Stream Plots

Stream Plot with Annotations

A stream plot with annotations in Matplotlib involves creating a stream plot with additional annotations, such as arrows, text, or markers, to provide extra information about specific points or features in the vector field. Annotations help us to highlight the key features of the plot and enhance its analysis.

Example

Now, we are creating a stream plot with annotations. The vector field represents a saddle point. Annotations are added using the text() function to label specific points on the plot. In here, "Source" and "Sink" are labeled near appropriate locations in the vector field, providing additional information about the features of the flow −

import numpy as np
import matplotlib.pyplot as plt

# Creating a grid
x = np.linspace(-2, 2, 20)
y = np.linspace(-2, 2, 20)
X, Y = np.meshgrid(x, y)

# Defining a vector field (here, a saddle)
U = X
V = -Y

# Plotting the stream plot with annotations
plt.streamplot(X, Y, U, V, density=1.5, linewidth=1, color='cyan')

# Adding annotations at specific points
plt.text(-1, 1.5, 'Source', color='red', fontsize=10, ha='right')
plt.text(1, -1.5, 'Sink', color='blue', fontsize=10, ha='left')

plt.title('Stream Plot with Annotations')
plt.show()

Output

Output of the above code is as follows −

Stream Plot with Annotations

Stream Plot with Start Points

A stream plot with start points in Matplotlib involves creating a stream plot with specified starting points for the streamlines. This allows for a more controlled visualization of the flow patterns, highlighting specific regions of interest in the vector field.

Example

In the example below, we are creating a stream plot of the vector field with specified start points ([0.5, 0.5] and [-0.5, -0.5]), represented by arrow −

import matplotlib.pyplot as plt
import numpy as np

# Generating grid of points
x, y = np.meshgrid(np.linspace(-2, 2, 20), np.linspace(-2, 2, 20))

# Defining vector field (rotational field)
u = -y
v = x

# Specifying start points for streamlines
start_points = np.array([[0.5, 0.5], [-0.5, -0.5]])

# Creating a stream plot with start points
plt.streamplot(x, y, u, v, start_points=start_points, density=1.5, linewidth=1, arrowsize=1.5)

plt.title('Stream Plot with Start Points')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

Output

The output obtained is as shown below −

Stream Plot with Start Points
Advertisements