Reputation: 1048
I have the below data:
coll_prop_tenure coll_prop_12m coll_prop_6m coll_prop_3m
0.04 0.04 0.06 0.08
0 0 0 0
0 0 0 0
0.06 0.06 0.1 0
0.38 0.38 0.25 0
0.61 0.61 0.66 0.61
0.01 0.01 0.02 0.02
0.1 0.1 0.12 0.16
0.04 0.04 0.04 0.09
0.22 0.22 0.22 0.22
0.72 0.72 0.73 0.72
0.39 0.39 0.45 0.64
I am using distplot from seaborn to plot the distribution as below:
######################## density plot #########################################
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot( data[cols_viz[0]] , color="skyblue", ax=axes[0, 0])
print("Skewness: %f" % data[cols_viz[0]].skew())
print("Kurtosis: %f" % data[cols_viz[0]].kurt())
sns.distplot( data[cols_viz[1]] , color="olive", ax=axes[0, 1])
print("Skewness: %f" % data[cols_viz[1]].skew())
print("Kurtosis: %f" % data[cols_viz[1]].kurt())
sns.distplot( data[cols_viz[2]] , color="gold", ax=axes[1, 0])
sns.distplot( data[cols_viz[3]] , color="teal", ax=axes[1, 1])
plt.show()
This does give me the values but I want them to appear within the corresponding plots instead.
How can I do this? Can someone please help me on this!
Upvotes: 1
Views: 10254
Reputation: 712
You can use ax.text()
to print text directly on to plots.
I imported your DF as code and tweaked a few things:
for i, ax in enumerate(axes)
will let you loop thru each ax in axes as well as get a number that corresponds to the column number, but you have to add .reshape(-1)
to collapse a level in the ndarray in order to get i
to range from 1-4. .iloc[:,i]
will let you refer to proper column for each subplot'transform=ax.transAxes
as an argument for the ax.text()
command will let you scale the axes so that the position of the text box can always be the same; I used x=0.97 and y=0.91 to roughly get it in the top-rightHere is the DF:
data = pd.DataFrame({'coll_prop_tenure': {0: 0.04, 1: 0.0, 2: 0.0, 3: 0.06, 4: 0.38, 5: 0.61, 6: 0.01, 7: 0.1, 8: 0.04, 9: 0.22, 10: 0.72, 11: 0.39}, \
'coll_prop_12m': {0: 0.04, 1: 0.0, 2: 0.0, 3: 0.06, 4: 0.38, 5: 0.61, 6: 0.01, 7: 0.1, 8: 0.04, 9: 0.22, 10: 0.72, 11: 0.39}, \
'coll_prop_6m': {0: 0.06, 1: 0.0, 2: 0.0, 3: 0.1, 4: 0.25, 5: 0.66, 6: 0.02, 7: 0.12, 8: 0.04, 9: 0.22, 10: 0.73, 11: 0.45}, \
'coll_prop_3m': {0: 0.08, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.61, 6: 0.02, 7: 0.16, 8: 0.09, 9: 0.22, 10: 0.72, 11: 0.64}})
And here is the code:
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot(data.iloc[:,0], color="skyblue", ax=axes[0,0])
sns.distplot(data.iloc[:,1], color="olive", ax=axes[0,1])
sns.distplot(data.iloc[:,2], color="gold", ax=axes[1,0])
sns.distplot(data.iloc[:,3], color="teal", ax=axes[1,1])
for i, ax in enumerate(axes.reshape(-1)):
ax.text(x=0.97, y=0.97, transform=ax.transAxes, s="Skewness: %f" % data.iloc[:,i].skew(),\
fontweight='demibold', fontsize=10, verticalalignment='top', horizontalalignment='right',\
backgroundcolor='white', color='xkcd:poo brown')
ax.text(x=0.97, y=0.91, transform=ax.transAxes, s="Kurtosis: %f" % data.iloc[:,i].kurt(),\
fontweight='demibold', fontsize=10, verticalalignment='top', horizontalalignment='right',\
backgroundcolor='white', color='xkcd:dried blood')
plt.tight_layout()
Upvotes: 6