如何在使用Matplotlib Python绘制的金融图表中跳过空日期(周末)?

如何在使用Matplotlib Python绘制的金融图表中跳过空日期(周末)?

为了在matplotlib中的金融图表中跳过周末,我们可以迭代数据帧中的时间,并在星期五或六跳过绘图。

步骤

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

  • 创建一个以时间为 关键字 的数据帧。

  • 迭代压缩的数据帧索引和时间。

  • 如果迭代的时间戳是一个星期五或六的日期,就不要绘制它们。

  • 绘制除星期五或六以外的点。

  • 设置Y轴的当前刻度位置。

  • 使用网格线布置一个图形。

  • 使用 show() 方法显示图像。

示例

import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame(dict(time=list(pd.date_range(start="2021-01-01", end="2021-01-15"))))
for i, t in zip(df.index, df.time):
   if t.weekday() in (5, 6):
      pass
   else:
      plt.plot(i, t, marker="*", ms=10)
plt.yticks(df.time)
plt.grid(True)
plt.show()

输出

如何在使用Matplotlib Python绘制的金融图表中跳过空日期(周末)?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程