Python Pyplot错误:“ValueError: x和y必须具有相同的第一个维度”

时间:2022-03-16 16:12:40

Can't get my simple intention of plotting 2 lists working. The length of the 1dim-lists are both the same and the list-values are simple floats with just digits after the comma. The long variable names may be suspect, since I've researched some cases where pyplot didn't like certain names. Anyway, the code looks like this (the list itself is printed + copypasted by me):

不能让我的简单意图绘制2个列表工作。1dim列表的长度是相同的,而list-values是简单的浮点数,仅在逗号之后。长变量名可能是可疑的,因为我研究了一些pyplot不喜欢某些名称的情况。不管怎样,代码是这样的(列表本身是打印出来的+我粘贴的):

countErrXPercent_allX_ABC = [0, 0.0, 0.0, 4.55, 5.41, 15.69, 23.44, 29.27, 32.05, 47.95, 48.0, 64.91, 43.94, 57.35, 52.27, 70.59, 79.55, 73.53, 77.14, 64.52, 77.78, 80.0, 82.61, 72.0, 86.96, 74.07, 77.78, 66.67, 60.0, 55.56, 80.0, 90.91, 77.78, 75.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, -1.0, 100.0, 100.0]
countErrYPercent_allY_ABC = [0, -1.0, 0.0, 10.0, 12.5, 22.22, 43.59, 48.61, 53.42, 67.37, 71.83, 78.65, 74.32, 72.88, 68.97, 82.69, 88.37, 77.27, 84.62, 85.0, 86.67, 90.0, 93.94, 83.33, 87.5, 80.95, 83.33, 92.86, 83.33, 57.14, 81.82, 100.0, 75.0, 100.0, 100.0, 60.0, 100.0, 50.0, 83.33, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]
P.plot(countErrXPercent_allX_ABC, '.', 'b', label='aErr < bErr')
P.plot(countErrYPercent_allY_ABC, '.', 'r', label='aErr < cErr')
P.show()

Error message:

错误信息:

Traceback (most recent call last):
File "xyz.py", line 169, in errClassesX
P.plot(countErrXPercent_allX_ABC, '.', 'b', label='aErr < bErr')
File "C:\Program Files (x86)\Python\lib\site-packages\matplotlib\pyplot.py", line 2817, in plot
ret = ax.plot(*args, **kwargs)
File "C:\Program Files (x86)\Python\lib\site-packages\matplotlib\axes.py", line 3996, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Program Files (x86)\Python\lib\site-packages\matplotlib\axes.py", line 330, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Program Files (x86)\Python\lib\site-packages\matplotlib\axes.py", line 308, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Program Files (x86)\Python\lib\site-packages\matplotlib\axes.py", line 248, in _xy_from_xy
raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension

The confusing thing is that when printing those lists + copypasting it in a new empty file (with new variables 'l' and 'k', pyplot works. I tried to plot duplicates (copy.copy() command) of the lists and assigning new varNames, but unfortunately without success. Therefore, any idea is highly appreciated.

令人困惑的是,当打印这些列表+将其复制到一个新的空文件中(使用新的变量“l”和“k”)时,pyplot可以工作。我尝试绘制列表的副本(copy.copy()命令)并分配新的varname,但不幸的是没有成功。因此,任何想法都受到高度赞赏。

1 个解决方案

#1


1  

You are fomatting the formatting string wrong, it should be:

您正在错误地设置格式字符串,应该是:

plt.plot(countErrXPercent_allX_ABC, '.b', label='aErr < bErr')

As you have it it is trying to plot '.' against your list.

正如你所看到的,它正在试图绘制。对你的列表。

It is also conventional to import pyplot as plt, not P.

通常将pyplot导入为plt,而不是P。

import matplotlib.pyplot as plt

Doing this will make your code easier for others to read and will make re-using example code easier on you.

这样做将使您的代码更容易被其他人阅读,并将使重用示例代码对您更容易。

#1


1  

You are fomatting the formatting string wrong, it should be:

您正在错误地设置格式字符串,应该是:

plt.plot(countErrXPercent_allX_ABC, '.b', label='aErr < bErr')

As you have it it is trying to plot '.' against your list.

正如你所看到的,它正在试图绘制。对你的列表。

It is also conventional to import pyplot as plt, not P.

通常将pyplot导入为plt,而不是P。

import matplotlib.pyplot as plt

Doing this will make your code easier for others to read and will make re-using example code easier on you.

这样做将使您的代码更容易被其他人阅读,并将使重用示例代码对您更容易。