Is there something similar like pandas DataFrame.hist() method that creates connected subplots for barcharts?


I like how the DataFrame method hist selects all numeric columns in a DataFrame and then simply returns a comprehensive plot of histogram subplots. With code as simple and effective as this:

df.hist(bins=50, figsize=(15,10))

enter image description here

But I can’t seem to create something similar for all categorical columns that simply returns a plot of barchart subplots.

df.select_dtypes("object").plot(kind="bar", subplots=True) # Error because not numeric values

Is there some variation for the code above so that it works? How is the subplots argument actually used?

Or is there else another similarly quicky and simple way to get what I’m seeking?

Thanks in advance!

Asked By: Dince-afk



You can try value_counts to get the count of each categorical variables:

import matplotlib.pyplot as plt
from itertools import zip_longest

cols = df.select_dtypes('object').columns
ncols = 3
nrows = len(cols) // ncols + (len(cols) % 3 != 0)
fig, axs = plt.subplots(nrows, ncols, figsize=(4*ncols, 4*nrows))

for col, ax in zip_longest(cols, axs.flat):
    if col:
        df[col].value_counts(sort=False).plot(kind='bar', ax=ax, rot=45, title=col)


enter image description here

Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.