Reputation: 193
I'm working with a large model ensemble. I'm calculating KDE probability distribution functions with pandas - at least for now it is the most feasible option since it automatically determines the (optimal?) bandwith. I'm comparing observations with a subset of models. Basically, I want the same observed pdf in 12 different sub panels so I can compare models and pdf better. This is my minimal example
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()
Is there any way I can 'copy/ duplicate' my first subplot - so
ax1 = df['obs'].plot.kde(ax=ax1, lw=2.0)
into the other panels without repeating the calculation? Alternatively can I somehow grab the values calculated? The reason why I don't want to repeat the computation is because it takes a lot of computing time with the original data.
Upvotes: 1
Views: 40
Reputation: 41327
Alternatively can I somehow grab the values calculated?
You can extract the line with Axes.get_lines()
and its values with 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)
Upvotes: 1