How to make the time from len of array

Question:

How to make time from len of array…

Picture 1:
enter image description here

Picture 2:
enter image description here

How to make the picture 1 as picture 2

Picture 1 come from below:

`

x=[]
colors=['red','green','brown','teal','gray','black','maroon','orange','purple']
colors2=['green','red','orange','black','maroon','teal','blue','gray','brown']
for i in range(0,1950):
    x.append(i)
for i in range(0,1):
    plt.figure(figsize=(15,6))
    # plt.figure()
    plt.plot(x,out[0:1950,i],color=colors[i])
    plt.plot(x,predictions[0:1950,i],markerfacecolor='none',color=colors2[i])
    plt.title('LSTM Regression (Training Data)')
    plt.ylabel('Force/Fz (N)')
    plt.xlabel('Time/s')
    plt.legend(['Real value', 'Predicted Value'], loc='upper right')
    plt.savefig('Regression Result.png'[i])
    plt.show()

`

Asked By: lead step

||

Answers:

Your x array is essentially just a number of iterations. The iteration number divided by whatever your time step is should give you time, so when calling plt.plot() use x/dt instead of x

Answered By: ahrensaj

@ahrensaj’s answer makes sense.

Here is your code modified to rescale the time scale.

x=[]
colors=['red','green','brown','teal','gray','black','maroon','orange','purple']
colors2=['green','red','orange','black','maroon','teal','blue','gray','brown']
# just update the x with correct scaling; use numpy array for faster computations
x = numpy.arange(0,1950)*40/1950 
for i in range(0,1):
    plt.figure(figsize=(15,6))
    # plt.figure()
    plt.plot(x,out[0:1950,i],color=colors[i])
    plt.plot(x,predictions[0:1950,i],markerfacecolor='none',color=colors2[i])
    plt.title('LSTM Regression (Training Data)')
    plt.ylabel('Force/Fz (N)')
    plt.xlabel('Time/s')
    plt.legend(['Real value', 'Predicted Value'], loc='upper right')
    plt.savefig('Regression Result.png'[i])
    plt.show()

EDIT:
@minn’s comment made me curious about np.linspace vs np.arange. I often use np.arange and divide appropriately because it is one less command to remember. In any case, I benchmarked the two. np.arange seems to do better.

In [8]: %time [np.linspace(0,40,1950) for _ in range(10)]
CPU times: user 1.81 ms, sys: 146 µs, total: 1.96 ms
Wall time: 1.6 ms
In [9]: %time [np.arange(0,1950)/40 for _ in range(10)]
CPU times: user 794 µs, sys: 55 µs, total: 849 µs
Wall time: 1.09 ms
Answered By: Umang Gupta
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.