
- Matplotlib - Home
- Matplotlib - Introduction
- Matplotlib - Vs Seaborn
- Matplotlib - Environment Setup
- Matplotlib - Anaconda distribution
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - Simple Plot
- Matplotlib - Saving Figures
- Matplotlib - Markers
- Matplotlib - Figures
- Matplotlib - Styles
- Matplotlib - Legends
- Matplotlib - Colors
- Matplotlib - Colormaps
- Matplotlib - Colormap Normalization
- Matplotlib - Choosing Colormaps
- Matplotlib - Colorbars
- Matplotlib - Working With Text
- Matplotlib - Text properties
- Matplotlib - Subplot Titles
- Matplotlib - Images
- Matplotlib - Image Masking
- Matplotlib - Annotations
- Matplotlib - Arrows
- Matplotlib - Fonts
- Matplotlib - Font Indexing
- Matplotlib - Font Properties
- Matplotlib - Scales
- Matplotlib - LaTeX
- Matplotlib - LaTeX Text Formatting in Annotations
- Matplotlib - PostScript
- Matplotlib - Mathematical Expressions
- Matplotlib - Animations
- Matplotlib - Celluloid Library
- Matplotlib - Blitting
- Matplotlib - Toolkits
- Matplotlib - Artists
- Matplotlib - Styling with Cycler
- Matplotlib - Paths
- Matplotlib - Path Effects
- Matplotlib - Transforms
- Matplotlib - Ticks and Tick Labels
- Matplotlib - Radian Ticks
- Matplotlib - Dateticks
- Matplotlib - Tick Formatters
- Matplotlib - Tick Locators
- Matplotlib - Basic Units
- Matplotlib - Autoscaling
- Matplotlib - Reverse Axes
- Matplotlib - Logarithmic Axes
- Matplotlib - Symlog
- Matplotlib - Unit Handling
- Matplotlib - Ellipse with Units
- Matplotlib - Spines
- Matplotlib - Axis Ranges
- Matplotlib - Axis Scales
- Matplotlib - Axis Ticks
- Matplotlib - Formatting Axes
- Matplotlib - Axes Class
- Matplotlib - Twin Axes
- Matplotlib - Figure Class
- Matplotlib - Multiplots
- Matplotlib - Grids
- Matplotlib - Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Subplots() Function
- Matplotlib - Subplot2grid() Function
- Matplotlib - Anchored Artists
- Matplotlib - Manual Contour
- Matplotlib - Coords Report
- Matplotlib - AGG filter
- Matplotlib - Ribbon Box
- Matplotlib - Fill Spiral
- Matplotlib - Findobj Demo
- Matplotlib - Hyperlinks
- Matplotlib - Image Thumbnail
- Matplotlib - Plotting with Keywords
- Matplotlib - Create Logo
- Matplotlib - Multipage PDF
- Matplotlib - Multiprocessing
- Matplotlib - Print Stdout
- Matplotlib - Compound Path
- Matplotlib - Sankey Class
- Matplotlib - MRI with EEG
- Matplotlib - Stylesheets
- Matplotlib - Background Colors
- Matplotlib - Basemap
- Matplotlib - Event Handling
- Matplotlib - Close Event
- Matplotlib - Mouse Move
- Matplotlib - Click Events
- Matplotlib - Scroll Event
- Matplotlib - Keypress Event
- Matplotlib - Pick Event
- Matplotlib - Looking Glass
- Matplotlib - Path Editor
- Matplotlib - Poly Editor
- Matplotlib - Timers
- Matplotlib - Viewlims
- Matplotlib - Zoom Window
- Matplotlib Widgets
- Matplotlib - Cursor Widget
- Matplotlib - Annotated Cursor
- Matplotlib - Buttons Widget
- Matplotlib - Check Buttons
- Matplotlib - Lasso Selector
- Matplotlib - Menu Widget
- Matplotlib - Mouse Cursor
- Matplotlib - Multicursor
- Matplotlib - Polygon Selector
- Matplotlib - Radio Buttons
- Matplotlib - RangeSlider
- Matplotlib - Rectangle Selector
- Matplotlib - Ellipse Selector
- Matplotlib - Slider Widget
- Matplotlib - Span Selector
- Matplotlib - Textbox
- Matplotlib Plotting
- Matplotlib - Line Plots
- Matplotlib - Area Plots
- Matplotlib - Bar Graphs
- Matplotlib - Histogram
- Matplotlib - Pie Chart
- Matplotlib - Scatter Plot
- Matplotlib - Box Plot
- Matplotlib - Arrow Demo
- Matplotlib - Fancy Boxes
- Matplotlib - Zorder Demo
- Matplotlib - Hatch Demo
- Matplotlib - Mmh Donuts
- Matplotlib - Ellipse Demo
- Matplotlib - Bezier Curve
- Matplotlib - Bubble Plots
- Matplotlib - Stacked Plots
- Matplotlib - Table Charts
- Matplotlib - Polar Charts
- Matplotlib - Hexagonal bin Plots
- Matplotlib - Violin Plot
- Matplotlib - Event Plot
- Matplotlib - Heatmap
- Matplotlib - Stairs Plots
- Matplotlib - Errorbar
- Matplotlib - Hinton Diagram
- Matplotlib - Contour Plot
- Matplotlib - Wireframe Plots
- Matplotlib - Surface Plots
- Matplotlib - Triangulations
- Matplotlib - Stream plot
- Matplotlib - Ishikawa Diagram
- Matplotlib - 3D Plotting
- Matplotlib - 3D Lines
- Matplotlib - 3D Scatter Plots
- Matplotlib - 3D Contour Plot
- Matplotlib - 3D Bar Plots
- Matplotlib - 3D Wireframe Plot
- Matplotlib - 3D Surface Plot
- Matplotlib - 3D Vignettes
- Matplotlib - 3D Volumes
- Matplotlib - 3D Voxels
- Matplotlib - Time Plots and Signals
- Matplotlib - Filled Plots
- Matplotlib - Step Plots
- Matplotlib - XKCD Style
- Matplotlib - Quiver Plot
- Matplotlib - Stem Plots
- Matplotlib - Visualizing Vectors
- Matplotlib - Audio Visualization
- Matplotlib - Audio Processing
- Matplotlib Useful Resources
- Matplotlib - Quick Guide
- Matplotlib - Cheatsheet
- Matplotlib - Useful Resources
- Matplotlib - Discussion
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 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 −

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 −

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 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 −
