当前有效matplotlib版本为:3.4.1。
概述
stem()函数的作用是绘制茎叶图(杆图、棉棒图、火柴杆图)。
茎叶图根据基线(baseline)上的位置(locs)绘制从基线到杆头(heads)的茎线,并在茎头(heads)处放置标记。
函数的签名为matplotlib.pyplot.stem(*args, linefmt=None, markerfmt=None, basefmt=None, bottom=0, label=None, use_line_collection=True, orientation=’vertical’, data=None)。
函数的调用签名为:stem([locs,] heads, linefmt=None, markerfmt=None, basefmt=None)。
函数的参数为:
locs:对于垂直茎叶图,该参数即茎线的x轴位置,对于水平茎叶图,该参数即茎线的y轴位置。类型为类数组结构,默认值为range(len(heads))。可选参数。heads:对于垂直茎叶图,该参数即茎头的y值,对于水平茎叶图,该参数即茎头的x值。类型为类数组结构。必备参数。linefmt:定义茎线的颜色和线形,取值同plot函数。类型为字符串,默认值为’C0-‘。可选参数。markerfmt:定义茎头标记的形状和颜色。类型为字符串,默认值为’C0o’。可选参数。basefmt:定义基线的格式字符串。类型为字符串,默认值为’C3-‘。 可选参数。orientation:茎叶图的方向。类型为字符串,默认值为’vertical’。可选参数。 当取值为’vertical’时,茎叶图为垂直方向; 当取值为’horizontal’时,则茎叶图为水平方向。bottom:基线的位置。类型为浮点数,默认值为0。可选参数。label:图例中使用的标签。类型为字符串,默认值None。 可选参数。use_line_collection:控制线条对象类型,即将被废弃。类型为布尔值,默认值True。 可选参数。
返回值为matplotlib.container.StemContainer对象,可以看做markerline, stemlines, baseline三元组。
案例:演示stem参数取值
import matplotlib.pyplot as pltimport numpy as np# 构造数据x = np.linspace(0.1, 2 * np.pi, 41)y = np.exp(np.sin(x))# 演示默认参数、默认样式plt.subplot(221)plt.stem(y)# 演示茎线、茎头、基线样式plt.subplot(222)plt.stem(x, y, linefmt=’r–‘, markerfmt=’gD’, basefmt=’b–‘, bottom=1)# 演示水平茎叶图plt.subplot(223)plt.stem(x, y, orientation=’horizontal’)# 演示stem返回值plt.subplot(224)markerline, stemlines, baseline = plt.stem(x, y)print(markerline.get_vps云服务器color(), baseline.get_color())plt.show() 69175776