Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas histogram: plot histogram for each column as subplot of a big figure

I am using the following code, trying to plot the histogram of every column of a my pandas data frame df_in as subplot of a big figure.

%matplotlib notebook
from itertools import combinations
import matplotlib.pyplot as plt

fig, axes = plt.subplots(len(df_in.columns) // 3, 3, figsize=(12, 48))
for x in df_in.columns:
    df_in.hist(column = x, bins = 100)

fig.tight_layout()

However, the histogram didn't show in the subplot. Any one knows what I missed? Thanks!

like image 313
Edamame Avatar asked Sep 22 '16 18:09

Edamame


People also ask

How can you plot histogram for a particular column Column_name of a Dataframe?

In order to plot a histogram using pandas, chain the . hist() function to the dataframe. This will return the histogram for each numeric column in the pandas dataframe.

How do you plot multiple histograms on the same plot?

For example, to make a plot with two histograms, we need to use pyplot's hist() function two times. Here we adjust the transparency with alpha parameter and specify a label for each variable. Here we customize our plot with two histograms with larger labels, title and legend using the label we defined.

How do you plot a histogram with different variables in Python?

plt. hist() method is used multiple times to create a figure of three overlapping histograms. we adjust opacity, color, and number of bins as needed. Three different columns from the data frame are taken as data for the histograms.


2 Answers

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

fig, axis = plt.subplots(2,3,figsize=(8, 8))
df_in.hist(ax=axis)

The above will plot a 2*3 (total 6 histogram for your dataframe). Adjust the rows and columns as per your arrangement requirements(# of columns)

My TA @Benjamin once told me , dataframe means do not have to use for loop.

like image 183
T D Avatar answered Sep 23 '22 17:09

T D


I can't comment burhan's answer because I don't have enough reputation points. The problem with his answer is that axes isn't one-dimensional, it contains axes triads, so it needs to be unrolled:

%matplotlib notebook
from itertools import combinations
import matplotlib.pyplot as plt

fig, axes = plt.subplots(len(df_in.columns)//3, 3, figsize=(12, 48))

i = 0
for triaxis in axes:
    for axis in triaxis:
        df_in.hist(column = df_in.columns[i], bins = 100, ax=axis)
        i = i+1
like image 26
Pascal Antoniou Avatar answered Sep 25 '22 17:09

Pascal Antoniou