【问题标题】:How do I plot a step function with Matplotlib in Python?如何在 Python 中使用 Matplotlib 绘制阶跃函数?
【发布时间】:2021-07-01 04:30:23
【问题描述】:

这应该很容易,但我刚刚开始玩 matplotlib 和 python。我可以做一条线或一个散点图,但我不知道如何做一个简单的阶梯函数。非常感谢任何帮助。

x = 1,2,3,4
y = 0.002871972681775004, 0.00514787917410944, 0.00863476098280219, 0.012003316194034325

【问题讨论】:

标签: python plot matplotlib


【解决方案1】:

您似乎想要step

例如

import matplotlib.pyplot as plt

x = [1,2,3,4] 
y = [0.002871972681775004, 0.00514787917410944, 
     0.00863476098280219, 0.012003316194034325]

plt.step(x, y)
plt.show()

【讨论】:

  • 好吧,如果您不想要任何垂直线,请查看plt.hlines。例如。 plt.hlines(y, range(1,5), range(2,6))
  • @Joe Kington:很抱歉一年后的评论。我对此有点困惑。图表不应该在 1 和 2 之间显示 0.0028,然后在 2 处跳转到 0.051,等等?看起来 step 使用下一个值。 (我正在考虑一个时间序列步骤,其中值是 t0 处的 a 并且在 t1 更改为 b 时保持 a 等。)有没有办法让 step() 以这种方式运行。
  • 为了回答我上面的评论,我发现您可以将 where='post' 参数添加到 step 函数中。所以在上面的例子中,它将是:plt.step(x, y, where='post')
【解决方案2】:

如果您有非均匀间隔的数据点,您可以为plot 使用drawstyle 关键字参数:

x = [1,2.5,3.5,4] 
y = [0.002871972681775004, 0.00514787917410944, 
     0.00863476098280219, 0.012003316194034325]

plt.plot(x, y, drawstyle='steps-pre')

还有steps-midsteps-post

【讨论】:

  • 太棒了。我用这个来做别的事情。简单而精确。
  • drawstyle documentation link“‘steps’等价于‘steps-pre’,并保持向后兼容。”
【解决方案3】:

matplotlib 3.4.0 中的新功能

有一个新的plt.stairs 方法来补充plt.step

plt.stairs 和底层的StepPatch 提供了一个更简洁的界面,用于为常见的您知道阶梯边缘的情况绘制阶梯常数函数。

这取代了plt.step 的许多用例,例如在绘制np.histogram 的输出时。

查看how to use plt.stairs and StepPatch 的官方 matplotlib 库。


何时使用 plt.stepplt.stairs

  • 如果您有参考点,请使用原始plt.step这里的步骤锚定在[1,2,3,4] 并扩展到左侧:

    plt.step(x=[1,2,3,4], y=[20,40,60,30])
    
  • 如果您有边缘,请使用新的plt.stairs。之前的[1,2,3,4] 台阶点对应[1,1,2,3,4] 楼梯边缘:

    plt.stairs(values=[20,40,60,30], edges=[1,1,2,3,4])
    


使用plt.stairsnp.histogram

由于np.histogram返回边,它直接与plt.stairs一起工作:

data = np.random.normal(5, 3, 3000)
bins = np.linspace(0, 10, 20)
hist, edges = np.histogram(data, bins)

plt.stairs(hist, edges)

【讨论】:

    【解决方案4】:

    我认为你想要pylab.bar(x,y,width=1) 或同样pyplot 的 bar 方法。如果没有,请查看 gallery 以了解您可以执行的多种绘图样式。每张图片都附带示例代码,向您展示如何使用 matplotlib 制作。

    【讨论】:

      【解决方案5】:

      画两条线,一条在 y=0 处,一条在 y=1 处,在你的 step 函数的任何 x 处截断。

      例如如果你想在 x=2.3 从 0 步到 1 并从 x=0 绘制到 x=5

      import matplotlib.pyplot as plt
      #                                 _
      # if you want the vertical line _|
      plt.plot([0,2.3,2.3,5],[0,0,1,1])
      #
      # OR:
      #                                       _
      # if you don't want the vertical line _
      #plt.plot([0,2.3],[0,0],[2.3,5],[1,1])
      
      # now change the y axis so we can actually see the line
      plt.ylim(-0.1,1.1)
      
      plt.show()
      

      【讨论】:

        【解决方案6】:

        如果有人只想逐步处理某些数据而不是实际绘制它:

        def get_x_y_steps(x, y, where="post"):
            if where == "post":
                x_step = [x[0]] + [_x for tup in zip(x, x)[1:] for _x in tup]
                y_step = [_y for tup in zip(y, y)[:-1] for _y in tup] + [y[-1]]
            elif where == "pre":
                x_step = [_x for tup in zip(x, x)[:-1] for _x in tup] + [x[-1]]
                y_step = [y[0]] + [_y for tup in zip(y, y)[1:] for _y in tup]
            return x_step, y_step
        

        【讨论】:

        • 一直在寻找内置函数,但没有成功。谢谢!我遇到了一个问题(由于版本?):TypeError: 'zip' object is not subscriptable。我正在使用 Python 3.5。该功能在我更改了 list(zip(...)) 的所有 zip 后起作用。
        • @daniel 感谢您的注意!我为 python 2 编写了代码,因此可以解释错误。
        猜你喜欢
        • 2019-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-01
        相关资源
        最近更新 更多