matplotlib错误:x和y必须是相同的大小,散点图关闭

发布于 2025-02-06 02:19:33 字数 1632 浏览 1 评论 0原文

试图查看多个功能之间的关系,但我一直遇到此错误。有人知道我如何解决吗?

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

Error scatter plot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咿呀咿呀哟 2025-02-13 02:19:33

出现错误的原因是您使用rest.pclass而不是failed.pclass

更新的代码

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')

输出图

”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文