How do I plot a step function with Matplotlib in Python?

后端 未结 5 1900
感动是毒
感动是毒 2020-12-09 02:32

This should be easy but I have just started toying with matplotlib and python. I can do a line or a scatter plot but i am not sure how to do a simple step function. Any help

相关标签:
5条回答
  • 2020-12-09 03:16

    In case someone just wants to stepify some data rather than actually plot it:

    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
    
    0 讨论(0)
  • 2020-12-09 03:22

    It seems like you want step.

    E.g.

    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()
    

    enter image description here

    0 讨论(0)
  • 2020-12-09 03:25

    I think you want pylab.bar(x,y,width=1) or equally pyplot's bar method. if not checkout the gallery for the many styles of plots you can do. Each image comes with example code showing you how to make it using matplotlib.

    0 讨论(0)
  • 2020-12-09 03:27

    If you have non-uniformly spaced data points, you can use the drawstyle keyword argument for plot:

    x = [1,2.5,3.5,4] 
    y = [0.002871972681775004, 0.00514787917410944, 
         0.00863476098280219, 0.012003316194034325]
    
    plt.plot(x, y, drawstyle='steps-pre')
    

    Also available are steps-mid and steps-post.

    0 讨论(0)
  • 2020-12-09 03:30

    Just draw two lines, one at y=0, and one at y=1, cutting off at whatever x your step function is for?

    e.g. if you want to step from 0 to 1 at x=2.3 and plot from x=0 to 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()
    
    0 讨论(0)
提交回复
热议问题