Home > Mobile >  How to make a graph with x and y of different length
How to make a graph with x and y of different length

Time:05-27

I'm trying to make a Python app that shows a graph after the input of the data by the user, but the problem is that the y_array and the x_array do not have the same dimensions. When I run the program, this error is raised:

ValueError: x and y must have same first dimension, but have shapes () and ()

How can I draw a graph with the X and Y axis of different length?

Here is a minimal example code that will lead to the same error I got :

import matplotlib.pyplot as plt

y = [0, 8, 9, 3, 0]
x = [1, 2, 3, 4, 5, 6, 7]

plt.plot(x, y)
plt.show()

CodePudding user response:

This is virtually a copy/paste of the answer found x_array

and for the y_array to be increased to the length of the x_array:

y_inter = interp.interp1d(np.arange(y.size), y)
y_ = y_inter(np.linspace(0,y.size-1,x.size))

print(len(x), len(y_))
# Prints 7,7
plt.plot(x,y_)
plt.show()

Which gives

y_array

  • Related