0

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?

6
  • Do you want all the histograms in one plot? Commented Nov 6, 2017 at 4:31
  • nope.... I want separate histogram for each attribute. But, in dataframe there are two labels 0 and 1. So, for each attribute, we have two histogram one for 0 and another for 1. I want these two histograms in one plot. Commented Nov 6, 2017 at 4:35
  • So you want 20 different plots? Commented Nov 6, 2017 at 4:36
  • yes... 20 different plots. Commented Nov 6, 2017 at 4:37
  • Please check my answer here. I guess it's quite the same. Commented Nov 6, 2017 at 4:39

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.