2015-03-25 37 views
0

讓我通過說我是一個Python /編程的新手。我需要爲我參加的一個研究課程做到這一點,這可能很粗糙。如何繪製pyplot中的特定函數以及顯示卡方值?

無論如何,我似乎無法理解如何使用pyplot繪製特定函數(函數在我的代碼中定義的其他地方,但是爲繪圖檢索它們很困難。另外,如何顯示卡方每個圖的價值? 謝謝!

# Chi-square HW 03/25/2015 

import numpy as np 
from scipy.stats import chi2 
from scipy.optimize import minimize 
import matplotlib.pyplot as plt 

x = np.arange(7) 
y = np.array([2, 6, 11, 15, 28, 41, 60]) 
sigma = np.array([2.1, 0.26, 0.31, 1.55, 0.42, 1.05, 2.1]) 


# Linear 
def lin(vars): 
    a,b,c = vars 
    lin = a +b*x 
    ymod = lin(x,a,b) 
    chisq = ((ymod - y)/sigma) **2 
    return np.sum(chisq) 
    print np.sum(chisq) 

# Quadratic 
def quad(vars): 
    a,b,c = vars 
    quad = a + b*x + c*x^2 
    ymod = quad(x,a,b,c) 
    chisq = ((ymod - y)/sigma) **2 
    return np.sum(chisq) 
    print np.sum(chisq) 

# Exponential 
def expn(vars): 
    a,b,c = vars 
    expn = a * exp(b*x) + c 
    ymod = expn(x,a,b,c) 
    chisq = ((ymod - y)/sigma) **2 
    return np.sum(chisq) 
    print np.sum(chisq) 

# Power-law 
def pwr(vars): 
    a,b,c = vars 
    pwr = a * x^b + c 
    ymod = pwr(x,a,b,c) 
    chisq = ((ymod - y)/sigma) **2 
    return np.sum(chisq) 
    print np.sum(chisq) 


#Minimization 
def LinRes(): 
    LinRes = minimize(lin, 0, vars, method='Powell') 
    return LinRes 

def QuadRes(): 
    QuadRes = minimize(quad, 0, vars, method='Powell') 
    return QuadRes 

def ExpnRes(): 
    ExpnRes = minimize(expn, 0, vars, method='Powell') 
    return ExpnRes 

def PwrRes(): 
    PwrRes = minimize(pwr, 0, vars, method='Powell') 
    return PwrRes 







#Linear Plot 
plt.figure(1) 
plt.errorbar(x,y,yerr=sigma, linestyle ='None') 
plt.plot(xmod,ymod,'k-') 
plt.title('Linear') 
plt.show() 
plt.savefig('Linear_Plot.png') 

#Quadratic Plot 
plt.figure(2) 
plt.errorbar(x,y,yerr=sigma, linestyle ='None') 
plt.plot(xmod,ymod,'k-') 
plt.title('Quadratic') 
plt.show() 
plt.savefig('Quadratic_Plot.png') 

#Exponential Plot 
plt.figure(3) 
plt.errorbar(x,y,yerr=sigma, linestyle ='None') 
plt.plot(xmod,ymod,'k-') 
plt.title('Exponential') 
plt.show() 
plt.savefig('Exponential_Plot.png') 

#Power-law Plot 
plt.figure(4) 
plt.errorbar(x,y,yerr=sigma, linestyle ='None') 
plt.plot(xmod,ymod,'k-') 
plt.title('Power-law') 
plt.show() 
plt.savefig('Power_Law_Plot.png') 
+0

你不繪製函數,你繪製數據 - 例如'plt.plot([1,2,3],[5,1,3])' - 所以你想*返回*來自您的功能的數據。或者傳入x值(如[1,2,3])並返回y值(例如[5,1,3])或者返回x值和y值(可以返回兩個值:'return x ,y')。 – cphlewis 2015-03-25 05:17:07

+0

另外,你的函數'lin','quad'等等自稱。你需要通過一個關於Python函數的簡單教程來理解這一點。 – cphlewis 2015-03-25 06:59:47

回答

0

低於糾正你的職責,你的情節,請看一看代碼(如果我理解你打算做正確的)。我沒有試圖解決您的最小化嘗試,但也許一些評論會幫助你

最明顯的更正錯誤如下:

  • 目前還不清楚您是否希望函數返回數據模型或卡方值的總和。一個建議是有一個函數來生成數據模型(ymod),另一個用來計算卡方和(參見下面的代碼示例)。
  • 函數中的打印語句必須在返回語句之前。否則它將永遠不會被代碼達到(而且是一個錯誤)。
  • plt.plot接收數據點,而不是函數。如果您想繪製func(x),則需要將數據對提供爲例如plt.plot(x,func(x))。

現在有點接近了。我相信你可以從那裏拿走它。

# Chi-square HW 03/25/2015 

import numpy as np 
import matplotlib.pyplot as plt 
from math import exp 
from scipy.stats import chi2 
from scipy.optimize import minimize 

x = np.arange(7) 
y = np.array([2, 6, 11, 15, 28, 41, 60]) 
sigma = np.array([2.1, 0.26, 0.31, 1.55, 0.42, 1.05, 2.1]) 


# Linear 
def lin(vars): 
    a,b,c = vars 
    return a + b*x 

# Quadratic 
def quad(vars): 
    a,b,c = vars 
    return a + b*x + c*x**2 

# Exponential 
def expn(vars): 
    a,b,c = vars 
    return a * np.exp(b*x) + c 

# Power-law 
def pwr(vars): 
    a,b,c = vars 
    return a * x**b + c 


# A generic function for chi-squared values could be the following 
def chisq_fun(fun, vars): 
    return np.sum(((fun(vars) - y)/sigma) ** 2) 





#Minimization 
def LinRes(): 
    LinRes = minimize(lin, 0, vars, method='Powell') 
    return LinRes 

def QuadRes(): 
    QuadRes = minimize(quad, 0, vars, method='Powell') 
    return QuadRes 

def ExpnRes(): 
    ExpnRes = minimize(expn, 0, vars, method='Powell') 
    return ExpnRes 

def PwrRes(): 
    PwrRes = minimize(pwr, 0, vars, method='Powell') 
    return PwrRes 




vars = (1.0, 2.0, 3.0) # (a, b, c) 

plt.title('Linear') 
plt.subplot(411) 
plt.xlabel('x (independent variable)') 
plt.ylabel('lin(%s, %s, %s)' % vars) 
plt.plot(x, lin(vars), 'o', linewidth=2.0, color='k') 
plt.errorbar(x, y, yerr=sigma, linestyle='None') 

plt.title('Quadratic') 
plt.subplot(412) 
plt.xlabel('x (independent variable)') 
plt.ylabel('quad(%s, %s, %s)' % vars) 
plt.plot(x, quad(vars), 'o', linewidth=2.0, color='k') 
plt.errorbar(x, y, yerr=sigma, linestyle='None') 

plt.title('Exponential') 
plt.subplot(413) 
plt.xlabel('x (independent variable)') 
plt.ylabel('expn(%s, %s, %s)' % vars) 
plt.plot(x, expn(vars), 'o', linewidth=2.0, color='k') 
plt.errorbar(x, y, yerr=sigma, linestyle='None') 

plt.title('Power-law') 
plt.subplot(414) 
plt.xlabel('x (independent variable)') 
plt.ylabel('pwr(%s, %s, %s)' % vars) 
plt.plot(x, pwr(vars), 'o', linewidth=2.0, color='k') 
plt.errorbar(x, y, yerr=sigma, linestyle='None') 

plt.show() 

(a, b, c) = vars 
print("Chi-squared sum for lin(%s, %s, %s) is %f" % (a, b, c, chisq_fun(lin, vars))) 
print("Chi-squared sum for quad(%s, %s, %s) is %f" % (a, b, c, chisq_fun(quad, vars))) 
print("Chi-squared sum for expn(%s, %s, %s) is %f" % (a, b, c, chisq_fun(expn, vars))) 
print("Chi-squared sum for pwr(%s, %s, %s) is %f" % (a, b, c, chisq_fun(pwr, vars)))