Matplotlib is a visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.
matplotlib.pyplot.stem() creates stem plots. A
Stem plot plots vertical lines at each x position covered under the graph from the baseline to y, and places a marker there.
Syntax: stem([x, ] y, linefmt=None, markerfmt=None, basefmt=None)
- x (array-like, optional): The x-positions of the stems. Default: (0, 1, …, len(y) – 1).
- y (array-like): The y-values of the stem heads.
- linefmt (str, optional): A string defining the properties of the vertical lines. Usually, this will be a color or a color and a linestyle:
- ‘-‘: solid line
- ‘–‘: dashed line
- ‘-.’: dash-dot line
- ‘:’ dotted line
Note: While it is technically possible to specify valid formats other than color or color and linestyle (e.g. ‘rx’ or ‘-.’), this is beyond the intention of the method and will most likely not result in a reasonable plot.
- markerfmt (str, optional): A string defining the properties of the markers at the stem heads. Default: ‘C0o’, i.e. filled circles with the first color of the color cycle.
- basefmt (str, optional): A format string defining the properties of the baseline.
Default: ‘C3-‘ (‘C2-‘ in classic mode).
- bottom (float, optional, default: 0): The y-position of the baseline.
- label (str, optional, default: None): The label to use for the stems in legends.
- use_line_collection (bool, optional, default: False): If True, store and plot the stem lines as a LineCollection instead of individual lines. This significantly increases performance, and will become the default option in Matplotlib 3.3. If False, defaults to the old behavior of using a list of Line2D objects.
The container may be treated like a tuple
(markerline, stemlines, baseline)
Example #1: Default plot
Stem plots vertical lines from a baseline to the y-coordinate and places a marker at the tip.
The position of the baseline can be adapted using bottom. The parameters linefmt, markerfmt, and basefmt control basic format properties of the plot. However, in contrast to
plot not all properties are configurable via keyword arguments. For more advanced control adapt the line objects returned by
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.