
- 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 - Stem Plots
A stem plot is a type of graph used to represent data where each data point is shown as a marker along a vertical line, called a stem, extending from a horizontal axis. The marker represents the value of the data point, while the position along the horizontal axis represents its location or category.
Imagine you are conducting a survey to collect the number of cars passing through a particular road intersection each hour of the day. You want to visualize this data to understand the traffic patterns. You can create a stem plot in this case, such that each hour of the day is represented along the horizontal axis, while the number of cars passing through is indicated by the length of the stems extending vertically from the corresponding hour −

Stem Plots in Matplotlib
A stem plot in Matplotlib is a visual representation of the frequency of data points along a vertical axis in two-dimensional space. We can use the stem() function within the 'pyplot' module to create a stem plot in Matplotlib. This function takes two parameters: the data point values defined by X and their frequency defined by Y. It then creates a vertical line between the X value and its corresponding Y value.
Lets start by drawing a basic stem plot.
Basic Stem Plot
A basic stem plot in Matplotlib is a simple way to visualize the frequency of data points along a vertical axis. The frequency of each data point is represented by a vertical line extending from the x-axis to the data value on the y-axis.
Example
In the following example, we are creating a basic stem plot. We use two arrays, 'x' and 'y', to represent the data points and their frequencies. Then, we use the stem() function to plot a vertical line at each x value extending to the corresponding y value −
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Creating a basic stem plot plt.stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-') plt.xlabel('X') plt.ylabel('Y') plt.title('Basic Stem Plot') plt.show()
Output
Following is the output of the above code −

Horizontal Stem Plot
In Matplotlib, a horizontal stem plot is similar to a basic stem plot, but the stems are oriented horizontally instead of vertically. This means that the frequency of each data point is represented by a horizontal line extending from the y-axis to the data value on the x-axis.
Example
In here, we are creating a horizontal stem plot. We again use two arrays, 'x' and 'y', to represent the data points and their frequencies. Then, we use the stem() function with a horizontal orientation to make the vertical axis as the x-axis and the horizontal axis as the y-axis. In the resultant plot, horizontal lines extend from the x value to the y value −
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Creating a stem plot with horizontal alignment plt.stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-', orientation='horizontal') plt.xlabel('Y') plt.ylabel('X') plt.title('Horizontal Stem Plot') plt.show()
Output
Output of the above code is as follows −

Sinusoidal Stem Plot
A sinusoidal stem plot in Matplotlib a way to visually represent data points that follow a sinusoidal pattern.
Imagine data points distributed along a sinusoidal curve. Instead of connecting these points with straight lines, a stem plot places vertical stems at each data point, and the height of each stem corresponds to the value of the data point.
Example
The following example creates a sinusoidal stem plot in Matplotlib using the stem() function. We start by creating evenly distributed data points and then find their frequencies by taking the sine value of each data point. The resultant plot creates a stem plot in a continuous form −
import matplotlib.pyplot as plt import numpy as np # Generating sinusoidal data x = np.linspace(0, 2*np.pi, 50) y = np.sin(x) # Creating a stem plot of sinusoidal data plt.stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-') plt.xlabel('X') plt.ylabel('Y') plt.title('Sinusoidal Stem Plot') plt.show()
Output
After executing the above code, we get the following output −

Triangular Stem Plot
A triangular stem plot in Matplotlib is a graphical representation where data points are shown as vertical stems rising from the x-axis, forming a triangular shape.
Each stem represents a data point, with its height indicating the value of the data point. Unlike a regular stem plot where stems are vertical lines, in a triangular stem plot, the stems have a triangular shape.
Example
Now, we are creating a triangular stem plot, where the leaf of the stem is a triangle instead of circle. We use arrays 'x' and 'y' to define the data points and their frequencies. To create a triangular leaf, we set the 'markerfmt' parameter (this parameter is used to define the shape of the leaves) to '^'. This results in a plot where the leaves of the steam are triangular −
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [1, 2, 3, 4, 5] # Creating a stem plot with triangular markers plt.stem(x, y, linefmt='g-', markerfmt='^b', basefmt='r-') plt.xlabel('X') plt.ylabel('Y') plt.title('Triangular Stem Plot') plt.show()
Output
The output obtained is as shown below −
