Reputation: 39
I am trying to use the plt.subplots method to plot 2 simple graphs with the same X axis.
data1 = np.load("data.npy") #loading numpy arrays to be plotted on y axis. both are same length.
data2 = np.load("data2.npy")
x1 = np.arange(100,len(fddata),100) # share x axis. set to have same length
fig, (ax1,ax2) = plt.subplots(nrows=2, ncols=1, sharex = True)
ax1.plot(x1, data1)
ax2.plot(x1, data2)
plt.show()
Using this code does not create a graph however. I get the error message " ValueError: x and y must have same first dimension, but have shapes (62,) and (18598,)". Even though my X and Y data should be the same size. I'm just wondering what could be causing this as I have gone through and checked but am unsure. Thank you.
Upvotes: 0
Views: 44
Reputation: 331
You x1 has to be definde like this.
x1=np.arange(len(data1))
Otherwise you get a much shorter array.
Or more genrally you can use
x1=np.linspace(x_min,x_max,len(data1))
which gives you equally spaced point on the interval [x_min,x_max]
Upvotes: 1