将图形复制到几个子图形中



我正在处理一个大型模型集成。我正在用panda计算KDE概率分布函数——至少目前它是最可行的选择,因为它可以自动确定(最优?(带宽。我正在将观察结果与模型的子集进行比较。基本上,我希望在12个不同的子面板中观察到相同的pdf,这样我就可以更好地比较模型和pdf。这是我的最小示例

import numpy as np
import pandas as pd
import xarray as xr
fig = plt.figure(0,figsize=(8.2,10.2))
fig.subplots_adjust(hspace=0.2)
fig.subplots_adjust(wspace=0.36)
fig.subplots_adjust(right=0.94)
fig.subplots_adjust(left=0.13)
fig.subplots_adjust(bottom=0.1)
fig.subplots_adjust(top=0.95)
plt.rcParams['text.usetex'] = False
plt.rcParams['axes.labelsize'] = 12
plt.rcParams['font.size'] = 11
plt.rcParams['legend.fontsize'] = 12
plt.rcParams['xtick.labelsize'] = 11
plt.rcParams['ytick.labelsize'] = 11
ax1 = fig.add_subplot(6,2,1)
ax2 = fig.add_subplot(6,2,2)
ax3 = fig.add_subplot(6,2,3)
ax4 = fig.add_subplot(6,2,4)
ax5 = fig.add_subplot(6,2,5)
ax6 = fig.add_subplot(6,2,6)
ax7 = fig.add_subplot(6,2,7)
ax8 = fig.add_subplot(6,2,8)
ax9 = fig.add_subplot(6,2,9)
ax10 = fig.add_subplot(6,2,10)
ax11 = fig.add_subplot(6,2,11)
ax12 = fig.add_subplot(6,2,12)
obs = np.array([448.2, 172.0881, 118.9816, 5.797349, 2, 0.7, 0.7, 0.1, 0.7, 14,
41.78181, 94.99255])
df= pd.DataFrame()
df['obs'] = obs
axes = [ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9,ax10,ax11,ax12]
for a in axes:
a = df['obs'].plot.kde(ax=a, lw=2.0)
plt.show()

有没有什么方法可以"复制/复制"我的第一个子图-所以

ax1 = df['obs'].plot.kde(ax=ax1, lw=2.0)

插入其他面板而不重复计算?或者,我可以以某种方式获取计算的值吗?我不想重复计算的原因是,使用原始数据需要大量的计算时间。

或者,我可以以某种方式获取计算的值吗?

您可以使用Axes.get_lines()提取行,并使用Line2D.get_data()来提取其值:

# plot KDE onto axes[0] (once)
df['obs'].plot.kde(ax=axes[0], lw=2.0)
# extract x and y from axes[0]
x, y = axes[0].get_lines()[0].get_data()
# plot x and y on remaining axes[1:]
for a in axes[1:]:
a.plot(x, y)

最新更新