python - 值误差 x 和 y 的大小必须相同,但它们是吗?

标签 python numpy matplotlib

我正在尝试绘制两个长度均为 10 的 numpy 数组:

mass_frac_plot = plt.figure()
mass_frac = mass_frac_plot.add_subplot(1, 1, 1)
mass_frac.scatter(mass_frac, homogen_frac)

我明白了:

Traceback (most recent call last):
  File "./plot.py", line 58, in <module>
    mass_frac.scatter(mass_frac, homogen_frac)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/__init__.py", line 1811, in inner
    return func(ax, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 3836, in scatter
    raise ValueError("x and y must be the same size")

救命啊!它们实际上都是数组,长度都是 10...

print(type(mass_frac), len(mass_frac))
print(mass_frac)
print(type(homogen_frac), len(homogen_frac))
print(homogen_frac)

给出这个:

<class 'numpy.ndarray'> 10
[ 3.67  3.6   4.45  3.74  4.93  4.35  3.89  5.62  4.73  3.83]
<class 'numpy.ndarray'> 10
[  98.02982123   96.82921968   88.8207858    83.37174016   75.55236146
87.71156752   91.95410515   66.34245085   77.63112123  119.74640558]

最佳答案

Help! They really are both arrays and of length 10...

不,他们不是:

mass_frac_plot = plt.figure()
mass_frac = mass_frac_plot.add_subplot(1, 1, 1)
^^^^^^^^^
mass_frac.scatter(mass_frac, homogen_frac)
                  ^^^^^^^^^

执行第二行后,mass_frac 显然不再是一个数组。相反,它将是类型

>>> type(mass_frac)
<class 'matplotlib.axes._subplots.AxesSubplot'>

更改变量名称,这样就不会破坏数组。

关于python - 值误差 x 和 y 的大小必须相同,但它们是吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37344240/

相关文章:

python - 即使在 Windows 10 中使用 pip 安装了 KivyCalendar,也会收到错误 : No module named 'calendar_ui' .

python - Pandas QuarterBegin() : Possible Bug when calculating First of quarter

python - 如何在 OpenCV 窗口中覆盖来自 Dlib 的面部关键点

Python:确保每个成对距离 >= 某个最小距离

python - 如何在 x 轴上仅显示 12 个数据点的月份

python - 如何提高 python 绘图的速度?

python - 如何删除列表列表中的 "\n"(python)

python - 无法杀死我的 python 代码。怎么了?

python - 错误: The truth value of an array with more than one element is ambiguous

python - 将 matplotlib 子图排列到子网格中