如何绘制从Python返回多个值的函数的输出? [英] How can I plot output from a function which returns multiple values in Python?

查看:463
本文介绍了如何绘制从Python返回多个值的函数的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据给定数据计算并绘制围绕脉冲星运行的行星的光谱能量.

I'm calculating and plotting spectral energy of planets orbiting pulsar from given data.

我以前已经在尺寸为[172,2](172行2列)的列表 variables 中对所有数据进行了排序.

I have previously ordered all data in a list variations with dimensions [172, 2] (172 rows and 2 columns).

首先,我必须计算一个预设模型的参数,并相应地从这些参数中计算光谱能量.

Firstly, I have to calculate parameters of a presupposed model and spectral energy accordingly (from those very parameters).

为此,我定义了一个函数,在其中定义了预设的模型,并定义了 find_fit 函数,该函数获取模型和变化数据.

To do that, I defined a function under which I defined the presupposed model and find_fit function which takes the model and variations data.

 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

然后我调用该函数并打印值:

Then I call the function and print values:

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 的输出.


Now I have to plot only sp_en output.

如果 spectrum 函数仅返回 sp_en ,则很容易.只需编写以下内容即可:

It is easy to that if a spectrum function return only sp_en. It is sufficient than to write:

var('t')
plot(spectrum(t), (t, 1, 100))

哪个返回: 能量-欧米茄图

接下来的问题是:如果要打印所有三个输出,如何绘制此函数?

The question than is: how do I plot this function if I want to print all three outputs?

推荐答案

只需对频谱的返回值使用索引:

Just use indexing on the return value from spectrum:

plot(spectrum(t)[2], (t, 1, 100))

这篇关于如何绘制从Python返回多个值的函数的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆