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

后端 未结 5 1911
感动是毒
感动是毒 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
    

提交回复
热议问题