如何使用 Matplotlib 在 Pandas 数据系列上绘制任意标记?

要在 Pandas 数据系列上绘制任意标记,我们可以使用标记。pyplot.plot()

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 使用轴标签(包括时间序列)制作 Pandas 数据系列。

  • 使用linestyle="dotted" 的plot()方法绘制系列索引。

  • 使用tick_params()方法旋转重叠标签。

  • 要显示图形,请使用show()方法。

示例

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

ts = pd.Series(np.random.randn(10),
index=pd.date_range('2021-04-10', periods=10))

plt.plot(ts.index, ts, '*', ls='dotted', color='red')

plt.tick_params(rotation=45)

plt.show()
输出结果