matplotlib错误:x和y必须是相同的大小,散点图关闭
试图查看多个功能之间的关系,但我一直遇到此错误。有人知道我如何解决吗?
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')
错误代码&散点图:
---------------------------------------------------------------------------
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出现错误的原因是您使用
rest.pclass
而不是failed.pclass
。更新的代码
输出图
The reason for your error is that you are using
survived.PClass
instead offailed.PClass
.Updated code
Output graph