A two-dimensional stem plot displays data as lines extending from a baseline along the x
-axis. A circle (the default) or the other marker's y
-position represents the data value that terminates each stem.
In this recipe, we will be discussing about how to create a stem plot.
Do not confuse stem with stem and leaf plots, which is a method of representing data by separating the last important digit of values as leaves and higher order values as stems.
For this kind of plot, we need to use a sequence of discrete data, where ordinary an line plots wouldn't make sense anyway.
Plot discrete sequences as stems, where data values are represented as markers at the end of each stem. Stems extend from baseline (usually at y=0) to the data point value.
We will use matplotlib to plot stem plots using the stem()
function. This function can use just a series of y
values when x
values are generated as a simple sequence from 0
to len(y)—1
. If we provide the stem function with both x
and y
sequences, they will be used for both axes.
What we want to configure with stem plot is several formatters:
linefmt
: This is the line formatter for stem linemarkerfmt
: The stems at the end of the line is formatted using this argumentbasefmt
: This formats the look of the base linelabel
: This defines label for legend for stem plothold
: This holds the current graphs on current axisbottom
: This sets up the location of baseline position on y axis, default value is 0
The hold
argument is used as a usual feature for plots. If it is on (True
), all the following plotting is added to the current axes. Otherwise, every plot will create a new figure and axes.
To create a stem plot, perform the following steps:
Here is the code to do it:
import matplotlib.pyplot as plt import numpy as np # time domain in which we sample x = np.linspace(0, 20, 50) # random function to simulate sampled signal y = np.sin(x + 1) + np.cos(x ** 2) # here we can setup baseline position bottom = -0.1 # True -- hold current axes for further plotting # False -- opposite. clear and use new figure/plot hold = False # set label for legend. label = "delta" markerline, stemlines, baseline = plt.stem(x, y, bottom=bottom, label=label, hold=hold) # we use setp() here to setup # multiple properties of lines generated by stem() plt.setp(markerline, color='red', marker='o') plt.setp(stemlines, color='blue', linestyle=':') plt.setp(baseline, color='grey', linewidth=2, linestyle='-') # draw a legend plt.legend() plt.show()
First, we need some data. For this recipe, the generated sampled pseudo-signal will suffice. In real world, any discrete sequential data can be properly visualized using stem plot. We generate this signal using Numpy's numpy.linspace
, numpy.cos
, and numpy.sin
functions.
We then set up label for stem plot and position of baseline, which defaults to 0.
If we want to draw multiple stem plots, we will set hold to True
, and the following plotting calls will be rendered over the same set of axes.
Call to a matplotlib.stem
returns three objects. First is markerline
, instance of Line2D
. This holds the reference to a line representing stems themselves, rendering only markers and not the line connecting the markers. This line can be made visible by the editing property of the Line2D
instance, which we will explain soon. The last one is also a Line2D
instance—baseline
, holding a reference to a horizontal line that represents the source of all stemlines
. Second object returned—stemlines
—collection (Python list at the moment) of Line2D
instances representing stem-lines, of course.
We use these objects returned to manipulate the visual appeal of the stem plot, using the setp
function to apply properties to all lines (Line2D
instances) in those objects or collections of objects.
Experiment with the desired settings until you understand how setp
changes your plot's style.