Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用Matplotlib绘制step函数?_Python_Plot_Matplotlib - Fatal编程技术网

如何在Python中使用Matplotlib绘制step函数?

如何在Python中使用Matplotlib绘制step函数?,python,plot,matplotlib,Python,Plot,Matplotlib,这应该很容易,但我刚刚开始玩弄matplotlib和python。我可以做一条直线或散点图,但我不知道如何做一个简单的步进函数。非常感谢您的帮助 x = 1,2,3,4 y = 0.002871972681775004, 0.00514787917410944, 0.00863476098280219, 0.012003316194034325 画两条线,一条在y=0,另一条在y=1,在步进函数的任何位置切断 e、 g.如果要在x=2.3处从0步进到1,并从x=0到x=5绘图: import

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

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

画两条线,一条在y=0,另一条在y=1,在步进函数的任何位置切断

e、 g.如果要在
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()

我认为您需要
pylab.bar(x,y,width=1)
或同样的
pyplot
的bar方法。如果没有,请检查可以执行的多种打印样式。每个图像都带有示例代码,显示了如何使用matplotlib制作图像。

这似乎是您想要的

例如


如果数据点间距不均匀,可以使用
绘图的关键字参数:

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

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

如果有人只是想对某些数据进行逐步处理,而不是实际绘制数据,还可以使用
中间步骤
后期步骤

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

你说的阶跃函数是什么意思?像直方图吗?@wim如果你不想要任何垂直线,请查看
plt.hlines
。例如,
plt.hlines(y,范围(1,5),范围(2,6))
@Joe Kington:很抱歉在一年后发表评论。我对此有点困惑。图表不应该在1和2之间显示0.0028,然后在2处跳到0.051,以此类推吗?看起来步骤使用了下一个值。(我正在考虑一个timeseries步骤,其中值在t0时为a,在t1变为b时保持a,依此类推。)是否有办法使step()以这种方式工作。为了回答我上面的评论,我发现可以将where='post'参数添加到step函数中。所以在上面的例子中,它应该是:
plt.step(x,y,where='post')
Great。我用这个做别的。简单而精确。。“'steps'相当于'steps pre',并且是为了向后兼容而维护的。”我们一直在寻找内置函数,但没有成功。谢谢我有一个问题(由于版本?):TypeError:“zip”对象不可订阅。我正在使用Python 3.5。在我更改列表的所有zip(zip(…))后,该函数可以正常工作。@daniel感谢您的提醒!我为Python2编写了代码,因此可以解释错误。
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