Matplotlib.pyplot.stem() in Python (original) (raw)
Last Updated : 21 Apr, 2020
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()
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)Parameters:
- 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. Returns :
container:
StemContainer
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.
Python3 `
importing libraries
import matplotlib.pyplot as plt import numpy as np
x = np.linspace(0.1, 2 * np.pi, 41) y = np.exp(np.sin(x))
plt.stem(x, y, use_line_collection = True) plt.show()
`
Output : **Example #2:**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 pyplot
.
Python `
importing libraries
import random import matplotlib.pyplot as plt
x = np.linspace(0.1, 2 * np.pi, 41) y = np.exp(np.sin(x))
markerline, stemlines, baseline = plt.stem( x, y, linefmt ='grey', markerfmt ='D', bottom = 1.1, use_line_collection = True)
markerline.set_markerfacecolor('none') plt.show()
`
Output:
Similar Reads
- Matplotlib.pyplot.sci() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, 2 min read
- Matplotlib.pyplot.sca() in Python Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, 1 min read
- Matplotlib.pyplot.title() in Python The title() method in the Matplotlib module is used to specify the title of the visualization depicted and display the title using various attributes. In this article, we will learn about this function with the help of examples. Syntax: matplotlib.pyplot.title(label, fontdict=None, loc='center', pad 3 min read
- Matplotlib.pyplot.xlim() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, 2 min read
- Matplotlib.pyplot.specgram() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.specgram() Function The specgram() function in pyplot module of matplotlib library is u 3 min read
- Matplotlib.pyplot.ylim() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.ylim() Function The ylim() function in pyplot module of matplotlib library is used to g 2 min read
- Matplotlib.pyplot.show() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. Sample Code - # sample code import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [16, 4, 1, 8]) plt.sho 2 min read
- Matplotlib.pyplot.semilogx() in Python Data Visualization Is an important part of analyzing the data as plotting graphs helps in providing better insight and understanding of the problem. Matplotlib.pyplot is one of the most commonly used libraries to do the same. It helps in creating attractive data and is super easy to use. Matplotlib 8 min read
- Matplotlib.pyplot.yscale() in Python Matplotlib Is a library in Python and it is a numerical - mathematical extension for the NumPy library. Pyplot Is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.yscale() in Python The matplotlib.pyplot.yscale() function in pyplot module of ma 2 min read
- Matplotlib.pyplot.twiny() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. Sample Code # sample code import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [16, 4, 1, 8]) plt.show( 2 min read