如何绘制 returns 多个 Python 值的函数的输出?
How can I plot output from a function which returns multiple values in Python?
简短介绍
我正在根据给定数据计算和绘制围绕脉冲星运行的行星的光谱能量。
我之前已经对列表 variations 中的所有数据进行了排序,尺寸为 [172, 2](172 行和 2 列)。
首先,我必须相应地计算预设模型的参数和光谱能量(从那些参数)。
为此,我定义了一个函数,在该函数下我定义了预设模型和 find_fit 函数,该函数采用模型和变体数据。
代码
var('a, b, t')
def spectrum(omega):
model = a*sin(omega*t) + b*cos(omega*t)
fit = find_fit(variations, model, parameters= [a, b], variables = [t], solution_dict = True)
sp_en = ((fit[a])**2 + (fit[b])**2)/2
return fit[a], fit[b], sp_en
然后我调用函数并打印值:
c, v, energy = spectrum(20) #enter arbitray angular frequency here
print "Values for the given angular frequency : \n \n a = %f, b = %f, spectral_energy = %f " % (c, v, energy)
现在我只需要绘制sp_en输出。
"Semi-solution"
很容易,如果spectrum函数return only sp_en 。比写就足够了:
var('t')
plot(spectrum(t), (t, 1, 100))
其中 returns:
energy-omega plot
问题是:如果我想打印所有三个输出,我该如何绘制这个函数?
只用 energy
变量调用 plot 函数
omega=10
c, v, energy = spectrum(omega) #enter arbitray angular frequency here
print "Values for the given angular frequency : \n \n a = %f, b = %f, spectral_energy = %f " % (c, v, energy)
plot(energy, (omega, 1, 100)
)
只需对频谱中的 return 值使用索引:
plot(spectrum(t)[2], (t, 1, 100))
简短介绍
我正在根据给定数据计算和绘制围绕脉冲星运行的行星的光谱能量。
我之前已经对列表 variations 中的所有数据进行了排序,尺寸为 [172, 2](172 行和 2 列)。
首先,我必须相应地计算预设模型的参数和光谱能量(从那些参数)。
为此,我定义了一个函数,在该函数下我定义了预设模型和 find_fit 函数,该函数采用模型和变体数据。
代码
var('a, b, t')
def spectrum(omega):
model = a*sin(omega*t) + b*cos(omega*t)
fit = find_fit(variations, model, parameters= [a, b], variables = [t], solution_dict = True)
sp_en = ((fit[a])**2 + (fit[b])**2)/2
return fit[a], fit[b], sp_en
然后我调用函数并打印值:
c, v, energy = spectrum(20) #enter arbitray angular frequency here
print "Values for the given angular frequency : \n \n a = %f, b = %f, spectral_energy = %f " % (c, v, energy)
现在我只需要绘制sp_en输出。
"Semi-solution"
很容易,如果spectrum函数return only sp_en 。比写就足够了:
var('t')
plot(spectrum(t), (t, 1, 100))
其中 returns: energy-omega plot
问题是:如果我想打印所有三个输出,我该如何绘制这个函数?
只用 energy
变量调用 plot 函数
omega=10
c, v, energy = spectrum(omega) #enter arbitray angular frequency here
print "Values for the given angular frequency : \n \n a = %f, b = %f, spectral_energy = %f " % (c, v, energy)
plot(energy, (omega, 1, 100)
)
只需对频谱中的 return 值使用索引:
plot(spectrum(t)[2], (t, 1, 100))