Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pyplot中绘制特定函数以及显示卡方值?_Python_Matplotlib_Chi Squared - Fatal编程技术网

Python 如何在pyplot中绘制特定函数以及显示卡方值?

Python 如何在pyplot中绘制特定函数以及显示卡方值?,python,matplotlib,chi-squared,Python,Matplotlib,Chi Squared,让我先说一下,我是Python/编程的新手。我需要为我正在学习的一门研究课程做这件事,这可能很艰难 无论如何,我似乎不明白如何使用pyplot绘制特定函数(这些函数在我的代码中的其他地方定义,但检索它们以进行绘制是很困难的。此外,如何在每个图形上显示卡方值? 谢谢 请看下面的代码,它修正了您的函数和绘图(如果我正确理解了您的意图)。我没有尝试修复您的最小化尝试,但这些注释可能会对您有所帮助 最明显的纠正错误如下: 不清楚您希望函数返回数据模型还是卡方值之和。建议使用一个函数来生成数据模型(ym

让我先说一下,我是Python/编程的新手。我需要为我正在学习的一门研究课程做这件事,这可能很艰难

无论如何,我似乎不明白如何使用pyplot绘制特定函数(这些函数在我的代码中的其他地方定义,但检索它们以进行绘制是很困难的。此外,如何在每个图形上显示卡方值? 谢谢


请看下面的代码,它修正了您的函数和绘图(如果我正确理解了您的意图)。我没有尝试修复您的最小化尝试,但这些注释可能会对您有所帮助

最明显的纠正错误如下:

  • 不清楚您希望函数返回数据模型还是卡方值之和。建议使用一个函数来生成数据模型(ymod),另一个函数来计算卡方和(请参见下面代码中的示例)
  • 函数中的print语句必须在return语句之前,否则代码将永远无法到达它(而且是一个错误)
  • 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)))

您不打印函数,而是打印数据——例如,
plt.plot([1,2,3],[5,1,3])
——因此您希望从函数返回数据。要么传入x值(如[1,2,3]),要么返回y值(如[5,1,3]),要么同时返回x值和y值(可以返回两个值:
返回x,y
)。此外,您的函数
lin
quad
等都可以自己调用。您需要通过一个简单的Python函数教程来理解这一点。
# 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)))