matplotlib中的步进函数 [英] Step function in matplotlib

查看:136
本文介绍了matplotlib中的步进函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到有关matplotlib中的步函数的一些问题,但这是不同的. 这是我的功能:

I have seen some questions asked about step functions in matplotlib but this one is different. Here is my function:

def JerkFunction(listOfJerk):
    '''Return the plot of a sequence of jerk'''
    #initialization of the jerk
    x = np.linspace(0,5,4)
    y = listOfJerk #step signal

    plt.axis([0,5,-2,2])
    plt.step(x,y,'y') #step display
    plt.xlabel('Time (s)')
    plt.ylabel('Jerk (m/s^3)')

    plt.title('Jerk produced by the engine')

    return plt.show()

我希望获得在放入JerkFunction([1,1,-1,1])时获得的曲线,但是通过输入:[1,-1,1,-1],实际上,在一开始,在实际情况下,加速度率值为0,而在t=0处,它变为jerk=+1,然后在t= 1处是Jerk=-1等.

I would like to have the curve obtained when I put JerkFunction([1,1,-1,1]) but by entering: [1,-1,1,-1], indeed, at the beginning, in a real case, the jerk value is 0 and at t=0, it becomes jerk=+1, then at t=1 it is Jerk=-1 etc.

推荐答案

我认为您对此问题有相同的问题

I think you are having the same problem this question Matlibplot step function index 0. The issue you are having is related to where step changes the value in relation to the x values (doc).

以下内容演示了实现此目的的三种方法.为了清楚起见,曲线垂直移动.水平虚线是零",垂直虚线是您的x值.

The following demonstrates the three ways it can do this. The curves are shifted vertically for clarity. The horizontal dashed lines are 'zero' and the vertical dotted lines are your x values.

x = np.linspace(0,5,3)
y = np.array([1,-1,1])

fig = plt.figure()
ax = fig.add_subplot(111)
ax.step(x,y,color='r',label='pre')
ax.step(x,y+3,color='b',label='post',where='post')
ax.step(x,y+6,color='g',label='mid',where='mid')
for j in [0,3,6]:
    ax.axhline(j,color='k',linestyle='--')
for j in x:
    ax.axvline(j,color='k',linestyle=':')
ax.set_ylim([-2,9])
ax.set_xlim([-1,6])
ax.legend()

ax.draw()

这篇关于matplotlib中的步进函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆