29

I have a pandas dataframe who just has numeric columns, and I am trying to create a separate histogram for all the features

ind group people value value_50
 1      1    5    100    1
 1      2    2    90     1
 2      1    10   80     1
 2      2    20   40     0
 3      1    7    10     0
 3      2    23   30     0

but in my real life data there are 50+ columns, how can I create a separate plot for all of them

I have tried

df.plot.hist( subplots = True, grid = True)

It gave me an overlapping unclear plot.

how can I arrange them using pandas subplots = True. Below example can help me to get graphs in (2,2) grid for four columns. But its a long method for all 50 columns

fig, [(ax1,ax2),(ax3,ax4)]  = plt.subplots(2,2, figsize = (20,10))
6
  • you want 50+ histograms in 4 subplots? Commented Apr 8, 2019 at 7:02
  • Did you use tight layout? Commented Apr 8, 2019 at 7:10
  • @goyo not 4 subplots that was just an example Commented Apr 8, 2019 at 7:57
  • So it was an example of what you don't want. But what's what you want? How should the figure look like? Commented Apr 8, 2019 at 7:59
  • @goyo histogram graph for all the columns in a data frame, simple! In the above example, its 5 columns, in my real example it's 50. Can you write something generic? I am sure it can be done Commented Apr 8, 2019 at 8:06

5 Answers 5

76

Pandas subplots=True will arange the axes in a single column.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame(np.random.rand(7,20))

df.plot(subplots=True)

plt.tight_layout()
plt.show()

enter image description here

Here, tight_layout isn't applied, because the figure is too small to arange the axes nicely. One can use a bigger figure (figsize=(...)) though.

In order to have the axes on a grid, one can use the layout parameter, e.g.

df.plot(subplots=True, layout=(4,5))

enter image description here

The same can be achieved if creating the axes via plt.subplots()

fig, axes = plt.subplots(nrows=4, ncols=5)
df.plot(subplots=True, ax=axes)
Sign up to request clarification or add additional context in comments.

1 Comment

@ ImportanceOfBeingErnest Could you figure out how to handle X and Y axis scale in each plot? !Here's the question
15

If you want to plot them separately (which is why I ended up here), you can use

for i in df.columns:
    plt.figure()
    plt.hist(df[i])

Comments

11

An alternative for this task can be using the "hist" method with hyperparameter "layout". Example using part of the code provided by @ImportanceOfBeingErnest:

import numpy as np
import matplotlib.pyplot as plt
import pandas  as pd

df = pd.DataFrame(np.random.rand(7,20))

df.hist(layout=(5,4), figsize=(15,10))

plt.show()

Comments

4

Using pandas.DataFrame I would suggest using pandas.DataFrame.apply. With a custom function, in this example plot(), you can print and save each figure seperately.

def plot(col):
 
    fig, ax = plt.subplots()
    ax.plot(col)
    plt.show()

df.apply(plot)

Comments

0

While not asked for in the question I thought I'd add that using the x parameter to plot would allow you to specify a column for the x axis data.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame(np.random.rand(7,20),columns=list('abcdefghijklmnopqrst'))
df.plot(x='a',subplots=True, layout=(4,5))    

plt.tight_layout()
plt.show()

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html

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.