将图复制到几个子图中

2024-05-18 07:13:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在和一个大型模特合奏。我正在用pandas计算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)

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


1条回答
网友
1楼 · 发布于 2024-05-18 07:13:19

Alternatively can I somehow grab the values calculated?

您可以使用^{}提取行,并使用^{}提取其值:

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

相关问题 更多 >