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

matplotlibpythondata visualization

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

步骤

  • 设置图形大小并调整子图之间和周围的填充。
  • 制作带有轴标签(包括时间序列)的 Pandas 数据系列。
  • 使用 plot() 方法绘制系列索引,其中 linestyle="dotted"
  • 使用 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()

输出


相关文章