0

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?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712

2 Answers2

1

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.

So instead of

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))

use it like

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()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

I think you can store the figure output in a variable and keep adding additional plots like you usually do using matplotlib. Here is a post of something similar to what you are asking.

Aditya Lahiri
  • 409
  • 3
  • 11