2017-09-09 70 views
1

我已經看到很多例子用於在曲線圖上顯示線性趨勢線的方程,但還沒有找到一個用於顯示更高階的曲線。我認爲它會是相似的,但我不斷收到錯誤。我有一種感覺,這與在我的印刷聲明中不理解z的目的有關。這是我的代碼,預先感謝!圖上的二次趨勢線方程?

import pandas as pd 
import matplotlib.pyplot as plt 
import numpy as np 


df=pd.read_csv('myfile', delimiter=',', usecols=[1,4], names=['Time','Position']) 

plt.figure(figsize=(9,6)) 
plt.suptitle('') 

x=df['Time'] 
y=df['Position'] 
plt.subplot(1,1,1) 
plt.tick_params(labelsize=6) 
plt.plot(x, y, 'o') 

z = np.polyfit(x, y, 2) 
p = np.poly1d(z) 
plt.plot(x,p(x),"r--") 

plt.xlabel('Time (s)', fontsize=9) 
plt.ylabel('Position (mm)', fontsize=9) 
plt.title('13.087 Degree Incline', fontsize=10, weight='bold') 
plt.legend(loc=2, prop={'size': 6}) 
plt.tight_layout() 

print "y=%.6fx^2+%.6fx+(%.6f)"%(z[0],z[1]) 

plt.show() 

編輯: 在python2運行的時候,我得到

File "scriptname.py", line 27, in <module> 
print "y=%.6fx^2+%.6fx+(%.6f)"%(z[0],z[1]) 
TypeError: not enough arguments for format string 

在python3運行時,我得到:

File "scriptname.py", line 27 
print "y=%.6fx^2+%.6fx+(%.6f)"%(z[0],z[1]) 
          ^
SyntaxError: invalid syntax 

回答

0

原則上的錯誤信息是在解釋什麼是錯的相當不錯。 在串"y=%.6fx^2+%.6fx+(%.6f)"你有3所格式說明,以後只提供2個參數(%(z[0],z[1])

溶液當然是提供儘可能多的參數作爲格式說明。

print "y=%.6fx^2+%.6fx+(%.6f)"%(z[0],z[1],z[2]) 

在python3你最終會落得同樣的錯誤,一旦你使用打印功能print(arg)而不是打印的聲明print arg在蟒蛇2

print ("y=%.6fx^2+%.6fx+(%.6f)"%(z[0],z[1],z[2]))