【问题标题】:Figure into a subplot matplotlib [closed]图为子图matplotlib [关闭]
【发布时间】:2018-12-03 19:02:47
【问题描述】:

我目前正在使用package,它在 python 中计算 SSA(奇异谱分析),它有一些函数可以将无花果作为输出。将一些数字放在我作为子图创建的数字中对我很有用。

有没有办法做到这一点?

【问题讨论】:

  • 那是哪个包?您需要一些解决方法,因为您的问题的一般答案是“否”。
  • 是蒙特卡洛奇异谱分析,在这个 repo github.com/VSainteuf/mcssa

标签: python matplotlib


【解决方案1】:

由于将内容从一个图形复制到另一个图形非常困难,并且通常不建议这样做,我建议您编写自己版本的mcssa package 的绘图功能。

所以不是

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

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

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

【讨论】:

    【解决方案2】:

    我认为您可以将图形输出存储在变量中,并像通常使用 matplotlib 一样继续添加额外的图。 Here 是与您所要求的内容类似的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 2022-12-03
      • 2020-12-15
      • 2016-06-01
      • 2017-09-08
      • 2015-07-15
      • 2023-03-12
      相关资源
      最近更新 更多