ramkrishna
ramkrishna

Reputation: 640

plot histogram for many columns quickly using groupby function of pandas dataframe

I have a pandas dataframe, which looks like enter image description here Here one of column is named label, that can take only two possible values 0 or 1.

I would like to make histogram for label 1 and for label 0 separately one top of other, like

enter image description here

I am able to make this for one of the column (named "MA_CL05") like:

temp = infile.groupby('label')
for k, v in temp:
  if k == 1:
    v.MA_CL05.hist(label='1',figsize=(15,15),bins=25,alpha=1.0,histtype = 'step',lw=4)
  if k == 0:
    v.MA_CL05.hist(label='0',figsize=(15,15),bins=25,alpha=1.0,histtype = 'step',lw=4)
plt.legend(loc=1, prop={'size': 51})
plt.show()

I can copy and past this patch for all of 20 columns and it will be fine. But, is there any easy way to plot this histogram of type (2) in one go?

Upvotes: 0

Views: 1077

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339280

You can add another loop, looping about the columns of the dataframe and specifying the axes to plot to.

fig, axes = plt.subplots(4,5)

for col,ax in zip(infile.columns[2:],axes.flatten()):
    temp = infile.groupby('label')
    for k, v in temp:
        v[col].hist(label=str(k),bins=25,alpha=1.0,histtype = 'step',lw=4, ax=ax)

plt.legend(loc=1, prop={'size': 51})
plt.show()

Upvotes: 1

Related Questions