matplotlib 步间填充函数

matplotlib fill in between step function

我需要在 matplotlib 中绘制一个阶跃函数(因为这是对我的数据的正确解释)并且想要在 x 轴和阶跃曲线之间进行填充。 类似于 fill_between,只是这个不适用于 drawstyle

请参阅下面的最小示例:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(2)
x = np.arange(50)
y = np.random.rand(50)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, drawstyle='steps-post')
ax.fill_between(x, y, drawstyle='steps-post')   # does not work
plt.show()

您可以使用 ax.fill_between(x, y, step='post')fill_between 没有 drawstyle 的参数。