注意
跳转到末尾以下载完整示例代码。
阶梯图演示#
本示例演示了stairs
用于阶梯常数函数。一个常见用例是直方图和类直方图数据可视化。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import StepPatch
np.random.seed(0)
h, edges = np.histogram(np.random.normal(5, 3, 5000),
bins=np.linspace(0, 10, 20))
fig, axs = plt.subplots(3, 1, figsize=(7, 15))
axs[0].stairs(h, edges, label='Simple histogram')
axs[0].stairs(h, edges + 5, baseline=50, label='Modified baseline')
axs[0].stairs(h, edges + 10, baseline=None, label='No edges')
axs[0].set_title("Step Histograms")
axs[1].stairs(np.arange(1, 6, 1), fill=True,
label='Filled histogram\nw/ automatic edges')
axs[1].stairs(np.arange(1, 6, 1)*0.3, np.arange(2, 8, 1),
orientation='horizontal', hatch='//',
label='Hatched histogram\nw/ horizontal orientation')
axs[1].set_title("Filled histogram")
patch = StepPatch(values=[1, 2, 3, 2, 1],
edges=range(1, 7),
label=('Patch derived underlying object\n'
'with default edge/facecolor behaviour'))
axs[2].add_patch(patch)
axs[2].set_xlim(0, 7)
axs[2].set_ylim(-1, 5)
axs[2].set_title("StepPatch artist")
for ax in axs:
ax.legend()
plt.show()

baseline可以接受一个数组,以允许绘制堆叠直方图。

pyplot.step
与pyplot.stairs
的比较#
pyplot.step
将阶梯的位置定义为单个值。阶梯从这些参考值向左/向右/向两侧延伸,具体取决于参数where。x和y值的数量相同。
相比之下,pyplot.stairs
通过其边界edges定义阶梯的位置,该边界比阶梯值多一个元素。
bins = np.arange(14)
centers = bins[:-1] + np.diff(bins) / 2
y = np.sin(centers / 2)
plt.step(bins[:-1], y, where='post', label='step(where="post")')
plt.plot(bins[:-1], y, 'o--', color='grey', alpha=0.3)
plt.stairs(y - 1, bins, baseline=None, label='stairs()')
plt.plot(centers, y - 1, 'o--', color='grey', alpha=0.3)
plt.plot(np.repeat(bins, 2), np.hstack([y[0], np.repeat(y, 2), y[-1]]) - 1,
'o', color='red', alpha=0.2)
plt.legend()
plt.title('step() vs. stairs()')
plt.show()

参考
本示例展示了以下函数、方法、类和模块的使用
脚本总运行时间:(0分钟2.670秒)