2014-11-01 221 views
23

我試圖擬合最適合我的matplotlib圖的線性線。我不斷收到x和y不具有相同第一維的錯誤。但他們都有15的長度。我做錯了什麼?Matplotlib:ValueError:x和y必須具有相同的第一維

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() 
+2

你是怎麼最終解決您的問題,只是轉換爲numpy的陣列? – 2017-09-28 19:33:13

回答

29

你應該讓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]) 

隨着這一變化,它產生的預期情節。如果他們是名單,m * x不會產生你期望的結果,但是是一個空的名單。請注意,m是標量,而不是標準Python float

我實際上認爲這有點可疑的Numpy行爲。在普通的Python,乘以一個整數的列表只是重複列表:

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

同時用浮動乘以一個列表給出了一個錯誤(因爲我認爲它應該):

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' 

怪異的事情是一個numpy的標量乘以一個Python列表顯然作品:

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] 

如此看來,浮子被截斷爲int,之後你會得到重複的次標準的Python行爲e列表,這是非常意想不到的行爲。最好的事情是提出一個錯誤(以便你自己發現了問題,而不必在Stackoverflow上提出你的問題),或者只是顯示預期的元素方式的乘法(你的代碼剛剛工作) 。有趣的是,列表和numpy的標量之間除了做工作:

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

等待我不明白,是什麼問題?只需轉換爲'np.array'修復了一些問題? – 2017-09-28 19:31:50

+0

@CharlieParker:是的,它的工作原理。這裏沒什麼奇怪的,讓我解釋一下。當OP做'm,b = np.polyfit(x,y,1)'時,這裏你得到'm'爲float64的值,例如'm = 0.642'。現在,當你試圖將一個浮點數「m」與一個列表「x」相乘時,你最終會得到一個空列表'[]'。這是因爲要將列表中的每個元素與一個值相乘,您需要將列表轉換爲'numpy數組'或使用內置的'map'函數。看到[這個答案](https://stackoverflow.com/questions/8194959/how-to-multiply-individual-elements-of-a-list-with-a-number)瞭解更多信息。 – ThePredator 2017-09-29 07:02:00

5

更改您的列表,以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() 

enter image description here

+0

這是爲什麼這樣工作?我覺得很奇怪。 – 2017-09-28 19:34:24

+0

@CharlieParker:是的,它的工作原理。這裏沒什麼奇怪的,讓我解釋一下。當OP做'm,b = np.polyfit(x,y,1)'時,這裏你得到'm'爲float64的值,例如'm = 0.642'。現在,當你試圖將一個浮點數「m」與一個列表「x」相乘時,你最終會得到一個空列表'[]'。這是因爲要將列表中的每個元素與一個值相乘,您需要將列表轉換爲'numpy數組'或使用內置的'map'函數。看到[這個答案](https://stackoverflow.com/questions/8194959/how-to-multiply-individual-elements-of-a-list-with-a-number)瞭解更多信息。 – ThePredator 2017-09-29 07:00:17

相關問題