Charith Jayasanka
Charith Jayasanka

Reputation: 4792

How to plot a graph out of two arrays with the x axis will be the length of the two arrays?

array1=[1,2,3,4]
array2=[5,6,7,8]

plt.plot(4, array1, 'g', label='Label 1')
plt.plot(4, array2, 'b', label='Label 2')
plt.title('Sample Graph')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.show()

But when I run this it says

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

Upvotes: 1

Views: 1980

Answers (1)

Sid
Sid

Reputation: 2189

4 is just an int. What you need is a list of 1-4

plt.plot(range(1,5), array1, 'g', label='Label 1')
plt.plot(range(1,5), array2, 'b', label='Label 2')

Output:

output

Upvotes: 1

Related Questions