matplotlib中的步骤函数

Pon*_*nzi 1 python matplotlib

我在matplotlib中看到了一些关于步骤函数的问题,但这个问题有所不同.这是我的功能:

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()
Run Code Online (Sandbox Code Playgroud)

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

tac*_*ell 5

我认为你有同样的问题这个问题Matlibplot步骤函数索引0.您遇到的问题与步骤更改值相对于x值(doc)的位置有关.

以下演示了它可以做到的三种方式.为清楚起见,曲线垂直移动.水平虚线为"零",垂直虚线为x值.

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()
Run Code Online (Sandbox Code Playgroud)

三步位置选项的示例