如何在Matplotlib Python中绘制一个茎图?
要在Matplotlib中绘制一个茎图,可以使用 stem() 方法。它会从基准线到Y坐标创建垂直线,并在顶部放置一个标记。
步骤
- 设置图形大小并调整子图之间和周围的填充。
- 使用numpy创建 x 和 y 数据点。
- 使用 stem() 方法创建茎图。
- 将标记面颜色设置为红色。
- 使用 show() 方法显示图形。
例子
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
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='*', bottom=1.1)
markerline.set_markerfacecolor('red')
plt.show()