File indexing completed on 2026-04-09 07:48:50
0001 """
0002
0003 https://stackoverflow.com/questions/8921296/how-do-i-plot-a-step-function-with-matplotlib-in-python
0004
0005 New in matplotlib 3.4.0
0006
0007 There is a new plt.stairs method to complement plt.step:
0008
0009
0010 """
0011 import numpy as np
0012 import matplotlib.pyplot as plt
0013
0014
0015 np.random.seed(0)
0016 h, edges = np.histogram(np.random.normal(5, 3, 5000),
0017 bins=np.linspace(0, 10, 20))
0018
0019 fig, axs = plt.subplots(3, 1, figsize=(7, 15))
0020 axs[0].stairs(h, edges, label='Simple histogram')
0021 axs[0].stairs(h, edges + 5, baseline=50, label='Modified baseline')
0022 axs[0].stairs(h, edges + 10, baseline=None, label='No edges')
0023 axs[0].set_title("Step Histograms")
0024
0025 axs[1].stairs(np.arange(1, 6, 1), fill=True,
0026 label='Filled histogram\nw/ automatic edges')
0027 axs[1].stairs(np.arange(1, 6, 1)*0.3, np.arange(2, 8, 1),
0028 orientation='horizontal', hatch='//',
0029 label='Hatched histogram\nw/ horizontal orientation')
0030 axs[1].set_title("Filled histogram")
0031
0032
0033 if 0:
0034 patch = StepPatch(values=[1, 2, 3, 2, 1],
0035 edges=range(1, 7),
0036 label=('Patch derived underlying object\n'
0037 'with default edge/facecolor behaviour'))
0038 axs[2].add_patch(patch)
0039 axs[2].set_xlim(0, 7)
0040 axs[2].set_ylim(-1, 5)
0041 axs[2].set_title("StepPatch artist")
0042
0043 for ax in axs:
0044 ax.legend()
0045 plt.show()