Python中的 Matplotlib.pyplot.stem()
Matplotlib 是Python中用于数组二维图的可视化库。 Matplotlib 是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用。
matplotlib.pyplot.stem()
matplotlib.pyplot.stem()
创建茎图。 Stem plot
在图表下覆盖的每个 x 位置绘制从基线到 y 的垂直线,并在那里放置一个标记。
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)
示例 #1:默认绘图
Stem 绘制从基线到 y 坐标的垂直线,并在尖端放置一个标记。
# 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()
输出 :
示例 #2:
基线的位置可以使用底部进行调整。参数linefmt、markerfmt和basefmt控制绘图的基本格式属性。但是,与plot
相比,并非所有属性都可以通过关键字参数进行配置。对于更高级的控制,请调整pyplot
返回的线对象。
# 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()
输出: