繁体   English   中英

使用从函数返回的图作为 Python 中的子图

[英]Using plots returned from functions as subplots in Python

我在 Python 上遇到的大部分问题都与密谋有关……

在我的代码中,我有两个函数,每个函数都有一个以上的 output。 这些函数返回,例如, plot_periodogram function 返回 best_period、period_error,并且在使用时返回 plot。另一个 function fold_LC在使用时返回 plot 作为结果。

我想要做的是将这些结果图用作图中的子图。 我试图将这些图保存在变量中并将它们返回到 plot,它们在函数的轴中,以及我在 inte.net 上找到的所有解决方案,但没有任何效果。 这是一个例子: IPython/matplotlib: Return subplot from function

有什么方法可以将在这些函数中获得的图存储在变量中,然后将它们用作子图?

这里有我的代码中的两个函数。

def plot_periodogram(curva_corregida):

gls = Gls(lc,fbeg=None, fend=None,Pbeg=min_t,Pend=max_t)    
best_period = gls.best['P']
period_error = gls.best['e_P']
period = 1/gls.freq
power = gls.power
max_power = power.max()
plt.plot(period,power,'b-',linewidth=.8)
plt.scatter(best_period,max_power,c='r',s=4,label='P={0} d'.format(round(best_period,4)))
plt.legend(loc='best')

return best_period, period_error


def fold_LC(curva_corregida, best_period):

folded = curva_corregida.fold(period = best_period)
lc_flux_folded = folded.flux
lc_time_folded = folded.phase    
plt.scatter(lc_time_folded,lc_flux_folded,s=0.5, color='b')

标准方法与您尝试做的相反。
不要从 function 返回子图,而是将子图传递给它。

import numpy as np, matplotlib.pyplot as plt

def plot_periodogram(subplot, curva_corregida):

    gls = Gls(lc,fbeg=None, fend=None,Pbeg=min_t,Pend=max_t)    
    best_period = gls.best['P']
    period_error = gls.best['e_P']
    period = 1/gls.freq
    power = gls.power
    max_power = power.max()
    subplot.plot(period,power,'b-',linewidth=.8)
    subplot.scatter(best_period,max_power,c='r',s=4,label='P={0} d'.format(round(best_period,4)))
    subplot.legend(loc='best')

fig, subplots = plt.subplots(nrows=2, ncols=3)
for n, subplot in enumerate(subplots.flatten()):
    plot_periodogram(subplot, curva_corregida(n))

请记住,您可以使用plt命名空间中的函数执行的所有操作也可以使用 Matplotlib 为您提供的图形对象的方法来完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM