图成子图matplotlib [英] Figure into a subplot matplotlib

查看:71
本文介绍了图成子图matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 在 Python 和它具有一些功能,可将无花果作为输出.将一些图形放在作为子图创建的图形中对我来说很有用.

I'm currently working with a package that calculates SSA (singular spectrum analysis) in python and it has some functions that give a fig as an output. It would be useful for me to put some figures inside a figure I'm creating as a subplot.

有没有办法做到这一点?

Is there anyway to do that?

推荐答案

由于将一个图形的内容复制到另一个图形真的很困难,而且通常不推荐这样做,我建议您编写自己版本的绘图功能 mcssa .

Since it's really hard to copy content from one figure to another and it is in general not recommended, I would suggest you write your own version of the plotting function of the mcssa package.

所以不是

m = MCSSA(...)
m.run_mcssa(...)
m.plot()

您可以复制以下函数以将结果绘制到您指定的轴

you can copy the following function to plot the result to an axes you specify

def ssaplot(mc_ssa, freq_rank=True, ax=None):
    ax = ax or plt.gca()
    ax.set_yscale('log')
    ax.set_ylabel('Variance')
    ax.set_title('M: {}; cov: {}'.format(mc_ssa.M, mc_ssa.algo))

    if not freq_rank:
        x = [i for i in range(mc_ssa.M)]
        y = mc_ssa.values

        ax.set_xlabel('Eigenvalue Rank')
        ax.plot(y, marker='s', linewidth=0, color='r')

    else:
        x = mc_ssa.freqs[mc_ssa.freq_rank]
        y = mc_ssa.values[mc_ssa.freq_rank]

        ax.set_xlabel('Frequency (Cycle/t. unit)')
        ax.plot(x, y, marker='s', linewidth=0, color='r')

    if mc_ssa.ismc:
        errors = np.array(mc_ssa.stats.iloc[3:5, :])
        mean_suro = np.array(mc_ssa.stats.iloc[0, :])
        ax.errorbar(x, y=mean_suro, yerr=errors, fmt=None,
                     ecolor='k', elinewidth=.5, capsize=2.5)
        ax.set_title('M: {}; g: {}; a: {}; Ns: {}'.format(mc_ssa.M,
                                                       mc_ssa.ar.gamma,
                                                       mc_ssa.ar.alpha,
                                                       mc_ssa.n_suro))

像这样使用

import numpy as np
import matplotlib.pyplot as plt
from mcssa import MCSSA   # probably(?)

m1 = MCSSA(...)
m1.run_mcssa(...)
m2 = MCSSA(...)
m2.run_mcssa(...)

fig, (ax1, ax2) = plt.subplots(2)
ssaplot(m1, ax=ax1)
ssaplot(m2, ax=ax2)
plt.show()

这篇关于图成子图matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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