【问题标题】:"x and y must be the same size" issues in python, histogram graph and scatterpython、直方图和散点图中的“x 和 y 必须相同大小”问题
【发布时间】:2021-10-20 18:25:14
【问题描述】:

我的代码无法加载,因为出现了下面提到的信息。记住我的 ppthdf 变量是一个 1 列和 100000 行的 excel 文件。你至少能猜到我错了吗?

代码:

p = np.arange(0.001, 100000, 0.01)
counts, bins = np.histogram(ppthdf['PPth'], density=True)   
plt.scatter(p, counts, s=100, c='red')
plt.show()

问题:

ValueError: x 和 y 的大小必须相同

(其实上图是和直方图一起绘制的,但是我的直方图可以,就是不能在直方图上放点,这就是上面代码的原因。)

【问题讨论】:

  • 这一定是因为pcounts 的大小不一样绘制点。您可能希望确保生成的p 的数量等于counts 的大小。
  • 尝试通过print(counts.shape, p.shape)打印它们的形状,你会发现原因。很有可能,counts 只有 10 行,而您的 p 有 10000000 行。您是否正在尝试绘制直方图?
  • 是的,我想绘制直方图,但点在每个 bin 的顶部(散点图和 histo 的混合以可视化函数)。

标签: python matplotlib graphics histogram scatter-plot


【解决方案1】:

您的数组pcounts 的大小不同。
p 数组的大小为 10,000,000。
counts 的大小为 100,000。

你应该这样做:

p = np.arange(0,100000,1)

【讨论】:

  • 不成功。 :(但感谢您尝试帮助我!
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 2022-01-12
  • 2018-12-14
  • 2020-02-23
  • 2017-05-30
  • 2019-08-20
  • 2014-08-25
  • 2022-01-17
相关资源
最近更新 更多