Nathan Curtis
Nathan Curtis

Reputation: 57

How to create subplots with Pandas Scatter Matrix

So I'm trying to create a subplot of pandas scatter matrices but have run into a bit of a wall. I've looked around at other similar questions, but none of the answers seemed to fix it. Right now I'm just trying to create a subplot of 2 of them. I'm going to move to a two by two subplot at some point, but am just trying to start small just to get it working. Here is my code

df_SH = pd.DataFrame({'East_Pacific_SH':DJF_con_SH_east_finite_all, 
                   'West_Pacific_SH':DJF_con_SH_west_finite_all,
                   'Atl_SH':DJF_con_SH_atl_finite_all,
                  'Zonal_SH':DJF_con_SH_zonal_finite_all})

df_NH = pd.DataFrame({'East_Pacific_NH':DJF_con_NH_east_finite_all, 
                   'West_Pacific_NH':DJF_con_NH_west_finite_all,
                   'Atl_NH':DJF_con_NH_atl_finite_all,
                  'Zonal_NH':DJF_con_NH_zonal_finite_all})

region_name=np.array(['East_Pacific_SH', 'West_Pacific_SH', 'Atl_SH', 'Zonal_SH'])


plt.suptitle('Control Correlations')
plt.subplot(211)
axes = pd.scatter_matrix(df_SH, alpha=0.2, diagonal='kde')
corr = df_SH.corr().as_matrix()
for i, j in zip(*plt.np.triu_indices_from(axes, k=1)):
    axes[j, i].annotate("%.3f" %corr[j,i], (.8, .9), xycoords='axes fraction', ha='center', va='center')
plt.title('Control DJF SH', size = 15)
#plt.savefig(filename='DJF_SH_Control_Scatter.pdf', ftype='pdf')
#plt.show()

plt.subplot(212)
axes2 = pd.scatter_matrix(df_NH, alpha=0.2, diagonal='kde')
corr2 = df_NH.corr().as_matrix()
for i, j in zip(*plt.np.triu_indices_from(axes, k=1)):
    axes2[j, i].annotate("%.3f" %corr2[j,i], (.8, .9), xycoords='axes fraction', ha='center', va='center')
plt.title('Control DJF NH', size = 15)
#plt.savefig(filename='DJF_NH_Control_Scatter.pdf', ftype='pdf')
plt.show()

Here are the results

result

result

result

Upvotes: 1

Views: 2653

Answers (1)

cphlewis
cphlewis

Reputation: 16249

pandas doesn't currently do that, although it has a promising ax argument:

iris = pd.DataFrame.from_csv('iris.csv')
chicks = pd.DataFrame.from_csv('ChickWeight.csv')
import matplotlib.pyplot as plt

fig, axs = plt.subplots(2)

#iris.plot.scatter('PL', 'PW', ax = axs[0])        
#chicks.plot.scatter('Diet', 'Chick', ax = axs[1]) # This is fine.

pd.scatter_matrix(iris, ax=axs[0], alpha=0.2)
pd.scatter_matrix(chicks, ax=axs[1], alpha=0.2)    # This clears axes unexpectedly.

plt.savefig('two_pd_scatter.png')

gives warning

/usr/local/lib/python2.7/site-packages/pandas/tools/plotting.py:3303: UserWarning: To output multiple subplots, the figure containing the passed axes is being cleared

"is being cleared", UserWarning)

Note that it's specifically clearing the whole figure, not just the passed axes.

I'd fix this by generating four tight-margin, systematically named figures for the scatter matrices and set up a document (TeX, whatever) that imports those figures in the right places and with the right title.

Upvotes: 2

Related Questions