Reputation: 25416
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!
Upvotes: 15
Views: 28137
Reputation: 1069
Stumbled on this ancient post and figured I'd add a correct answer, the above answers are mostly right but ignore the fact that they need to +1 the flooring function. Somewhat addressed in @burhan 's answer but just cleaned up a bit here:
import seaborn as sns
from matplotlib import pyplot as plt
import math
def plot_univariate_distributions(df, numcols=3, fig_kwargs = dict()):
numrows = math.ceil(len(df.columns) / numcols)
fig, axes = plt.subplots(numrows, numcols, **fig_kwargs)
for col, ax in zip(df.columns, axes.flat):
sns.histplot(df[col], ax=ax)
ax.set_title(col, size=10)
plt.tight_layout()
plot_univariate_distributions(mydf, fig_kwargs={'figsize':[10,5]})
Upvotes: 0
Reputation: 1150
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.
Upvotes: 9
Reputation: 311
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
Upvotes: 14
Reputation: 924
You need to specify which axis you are plotting to. This should work:
fig, axes = plt.subplots(len(df_in.columns)//3, 3, figsize=(12, 48))
for col, axis in zip(df_in.columns, axes):
df_in.hist(column = col, bins = 100, ax=axis)
Upvotes: 3