在 python 中绘图时出现 ValueError

发布于 2024-11-30 13:25:11 字数 329 浏览 0 评论 0原文

我想使用 matplotlib 的绘图方法和绘图 2 数组。沿 x 轴绘制的数组有 1 行和 128 列 [1,128]。沿 y 轴绘制的数组有 14 行和 128 列 [14,128]。当我尝试使用绘图方法时,它返回以下消息:

ValueError: x and y must have same first dimension

这是我用来绘制它的代码。 ab 是 2 个数组。

line, = plt.plot(b, a, 'bs', markersize=4)

I want to use the plot method of the matplotlib and plot 2 arrays. The array to be plotted along the x-axis has 1 row and 128 columns [1,128]. The array to be plotted along the y-axis has 14 rows and 128 columns [14,128]. When I try to use the plot method, it returns this message:

ValueError: x and y must have same first dimension

This is the code that I am using to plot it. a and b are the 2 arrays.

line, = plt.plot(b, a, 'bs', markersize=4)

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

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

发布评论

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

评论(2

删除→记忆 2024-12-07 13:25:11

当 a 和 b 的大小(取自上面的示例)不同时,就会出现此错误 - 因此,此处的 128 个 x 值应针对 128 y 值进行绘制。

This error shows up when the size of a and b (taking from above example) is not the same - so, 128 x-values here should be plotted against 128 y-values.

白鸥掠海 2024-12-07 13:25:11

你只是把数组搞错了。转置它们,一切都应该正常。

>>> from matplotlib import pyplot as plt
>>> import numpy as np
>>> x = np.array(range(1,129))
>>> y = np.random.rand(14,128)
>>> plt.plot(x, y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2286, in plot 
    ret = ax.plot(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 3783, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 294, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 234, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
>>> plt.plot(x.T, y.T)
# works

You've just got your arrays the wrong way around. Transpose them and everything should work.

>>> from matplotlib import pyplot as plt
>>> import numpy as np
>>> x = np.array(range(1,129))
>>> y = np.random.rand(14,128)
>>> plt.plot(x, y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2286, in plot 
    ret = ax.plot(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 3783, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 294, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 234, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
>>> plt.plot(x.T, y.T)
# works
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文