简体   繁体   English

Matplotlib:ValueError:x 和 y 必须具有相同的第一维

[英]Matplotlib: ValueError: x and y must have same first dimension

I am trying to fit a linear line of best fit to my matplotlib graph.我正在尝试将最适合我的 matplotlib 图的线性线拟合。 I keep getting the error that x and y do not have the same first dimension.我不断收到 x 和 y 没有相同第一维的错误。 But they both have lengths of 15. What am I doing wrong?但它们的长度都是 15。我做错了什么?

import matplotlib.pyplot as plt
from scipy import stats
import numpy as np

x = [0.46,0.59,0.68,0.99,0.39,0.31,1.09,0.77,0.72,0.49,0.55,0.62,0.58,0.88,0.78]
y = [0.315,0.383,0.452,0.650,0.279,0.215,0.727,0.512,0.478,0.335,0.365,0.424,0.390,0.585,0.511]
xerr = [0.01]*15
yerr = [0.001]*15

plt.rc('font', family='serif', size=13)
m, b = np.polyfit(x, y, 1)
plt.plot(x,y,'s',color='#0066FF')
plt.plot(x, m*x + b, 'r-') #BREAKS ON THIS LINE
plt.errorbar(x,y,xerr=xerr,yerr=0,linestyle="None",color='black')
plt.xlabel('$\Delta t$ $(s)$',fontsize=20)
plt.ylabel('$\Delta p$ $(hPa)$',fontsize=20)
plt.autoscale(enable=True, axis=u'both', tight=False)
plt.grid(False)
plt.xlim(0.2,1.2)
plt.ylim(0,0.8)
plt.show()

You should make x and y numpy arrays, not lists:您应该制作xy numpy 数组,而不是列表:

x = np.array([0.46,0.59,0.68,0.99,0.39,0.31,1.09,
              0.77,0.72,0.49,0.55,0.62,0.58,0.88,0.78])
y = np.array([0.315,0.383,0.452,0.650,0.279,0.215,0.727,0.512,
              0.478,0.335,0.365,0.424,0.390,0.585,0.511])

With this change, it produces the expect plot.通过此更改,它会生成期望图。 If they are lists, m * x will not produce the result you expect, but an empty list.如果它们是列表,则m * x不会产生您期望的结果,而是一个空列表。 Note that m is a numpy.float64 scalar, not a standard Python float .请注意, m是一个numpy.float64标量,而不是标准的 Python float

I actually consider this a bit dubious behavior of Numpy.我实际上认为这是 Numpy 的一个有点可疑的行为。 In normal Python, multiplying a list with an integer just repeats the list:在普通 Python 中,将列表与整数相乘只是重复列表:

In [42]: 2 * [1, 2, 3]
Out[42]: [1, 2, 3, 1, 2, 3]

while multiplying a list with a float gives an error (as I think it should):将列表与浮点数相乘会产生错误(我认为应该如此):

In [43]: 1.5 * [1, 2, 3]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-d710bb467cdd> in <module>()
----> 1 1.5 * [1, 2, 3]
TypeError: can't multiply sequence by non-int of type 'float'

The weird thing is that multiplying a Python list with a Numpy scalar apparently works:奇怪的是,将 Python 列表与 Numpy 标量相乘显然有效:

In [45]: np.float64(0.5) * [1, 2, 3]
Out[45]: []

In [46]: np.float64(1.5) * [1, 2, 3]
Out[46]: [1, 2, 3]

In [47]: np.float64(2.5) * [1, 2, 3]
Out[47]: [1, 2, 3, 1, 2, 3]

So it seems that the float gets truncated to an int, after which you get the standard Python behavior of repeating the list, which is quite unexpected behavior.因此,浮点数似乎被截断为整数,之后您将获得重复列表的标准 Python 行为,这是非常出乎意料的行为。 The best thing would have been to raise an error (so that you would have spotted the problem yourself instead of having to ask your question on Stackoverflow) or to just show the expected element-wise multiplication (in which your code would have just worked).最好的办法是提出一个错误(这样你就可以自己发现问题,而不必在 Stackoverflow 上提出你的问题)或者只显示预期的元素乘法(你的代码刚刚工作) . Interestingly, addition between a list and a Numpy scalar does work:有趣的是,列表和 Numpy 标量之间的加法确实有效:

In [69]: np.float64(0.123) + [1, 2, 3]
Out[69]: array([ 1.123,  2.123,  3.123])

Changing your lists to numpy arrays will do the job!!将您的列表更改为numpy数组即可!

import matplotlib.pyplot as plt
from scipy import stats
import numpy as np 

x = np.array([0.46,0.59,0.68,0.99,0.39,0.31,1.09,0.77,0.72,0.49,0.55,0.62,0.58,0.88,0.78]) # x is a numpy array now
y = np.array([0.315,0.383,0.452,0.650,0.279,0.215,0.727,0.512,0.478,0.335,0.365,0.424,0.390,0.585,0.511]) # y is a numpy array now
xerr = [0.01]*15
yerr = [0.001]*15

plt.rc('font', family='serif', size=13)
m, b = np.polyfit(x, y, 1)
plt.plot(x,y,'s',color='#0066FF')
plt.plot(x, m*x + b, 'r-') #BREAKS ON THIS LINE
plt.errorbar(x,y,xerr=xerr,yerr=0,linestyle="None",color='black')
plt.xlabel('$\Delta t$ $(s)$',fontsize=20)
plt.ylabel('$\Delta p$ $(hPa)$',fontsize=20)
plt.autoscale(enable=True, axis=u'both', tight=False)
plt.grid(False)
plt.xlim(0.2,1.2)
plt.ylim(0,0.8)
plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Matplotlib:ValueError:x和y必须具有相同的第一维错误 - Matplotlib: ValueError: x and y must have same first dimension Error Matplotlib 'ValueError: x 和 y 必须具有相同的第一维,但具有形状 (20,) 和 (1,)' - Matplotlib 'ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)' ValueError: x 和 y 必须具有相同的第一维,但有形状,tkinter 和 matplotlib 问题 - ValueError: x and y must have same first dimension, but have shapes, tkinter and matplotlib problem Matplotlib 中的 Plot K-Means:ValueError:x 和 y 必须具有相同的第一个维度,但具有形状 (10,) 和 (1,) - Plot K-Means in Matplotlib: ValueError: x and y must have same first dimension, but have shapes (10,) and (1,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 输入错误,然后输入值错误:x 和 y 必须具有相同的第一维 - Type error, and then ValueError: x and y must have same first dimension Python ValueError:x 和 y 必须具有相同的第一维 - Python ValueError: x and y must have same first dimension “ ValueError:x和y必须具有相同的第一维”的不同情况 - A different situation of “ValueError: x and y must have same first dimension”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM