Home > Blockchain >  Matplotlib Error: x and y must be the same size, scatter plot is off
Matplotlib Error: x and y must be the same size, scatter plot is off

Time:06-09

Trying to look at relationships between multiple features, but I keep getting this error. Does anyone know how I can fix it?

plt.figure(figsize=(10,6))
plt.scatter(survived.Fare, survived.Pclass, alpha =0.5, color = 'orange', label='Survived');
plt.scatter(failed.Fare, survived.Pclass, alpha =0.5, color = 'blue', label='Failed');
plt.title('Distribution of Pclass and Fare for Survived and Failed')
plt.xlabel('Fare')
plt.ylabel('Pclass')
plt.legend()
plt.savefig('Survived_and_not_survived.jpg')

The error code & scatter plot:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-121-21c6971751a4> in <module>()
      1 plt.figure(figsize=(10,6))
      2 plt.scatter(survived.Fare, survived.Pclass, alpha =0.5, color = 'orange', label='Survived');
----> 3 plt.scatter(failed.Fare, survived.Pclass, alpha =0.5, color = 'blue', label='Failed');
      4 plt.title('Distribution of Pclass and Fare for Survived and Failed')
      5 plt.xlabel('Fare')

3 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
   4389         y = np.ma.ravel(y)
   4390         if x.size != y.size:
-> 4391             raise ValueError("x and y must be the same size")
   4392 
   4393         if s is None:

ValueError: x and y must be the same size

Error scatter plot

CodePudding user response:

The reason for your error is that you are using survived.PClass instead of failed.PClass.

Updated code

plt.figure(figsize=(10,6))
plt.scatter(survived.Fare, survived.Pclass, alpha =0.5, color = 'orange', label='Survived');
plt.scatter(failed.Fare, failed.Pclass, alpha =0.5, color = 'blue', label='Failed');
plt.title('Distribution of Pclass and Fare for Survived and Failed')
plt.xlabel('Fare')
plt.ylabel('Pclass')
plt.legend()
plt.savefig('Survived_and_not_survived.jpg')

Output graph

enter image description here

  • Related