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

Matplotlib: ValueError: x and y must have same first dimension

我正在尝试最适合我的matplotlib图的线性线。 我不断收到x和y没有相同的第一维的错误。 但是它们的长度都为15。我在做什么错?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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()


您应该创建xy numpy数组,而不是列出:

1
2
3
4
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])

通过此更改,将生成期望图。如果它们是列表,则m * x不会产生您期望的结果,而是一个空列表。请注意,mnumpy.float64标量,而不是标准的Python float

我实际上认为这是Numpy的可疑行为。在普通的Python中,将列表与整数相乘只会重复该列表:

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

同时将列表与浮点数相乘会产生错误(我认为应该这样):

1
2
3
4
5
6
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'

奇怪的是,将Python列表与Numpy标量相乘显然有效:

1
2
3
4
5
6
7
8
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]

因此,似乎float会被截断为int,然后您得到重复列表的标准Python行为,这是非常意外的行为。最好的办法是引发一个错误(这样您就可以自己发现问题,而不必在Stackoverflow上提问)或仅显示预期的逐元素乘法(您的代码将在其中正常工作) 。有趣的是,列表与Numpy标量之间的加法确实起作用:

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


将您的列表更改为numpy数组就可以了!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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()

enter image description here