silent_hunter
silent_hunter

Reputation: 2508

Plotting histogram for categorical values

I am using seaborn and I want to plot only categorical values (sex,smoker,day,time,size) with facet histogram (total 5 histogram on same place).

    import seaborn as sns
    tips = sns.load_dataset("tips")
    tips
    
    # Plotting
    g = sns.FacetGrid(tips, col="sex",  row="smoker")
    g.map_dataframe(sns.histplot, x="time")

enter image description here

I try with this line of code but this doesn't give me the results I expected. So can anybody help me with how to solve this problem?

Upvotes: 0

Views: 1498

Answers (1)

JohanC
JohanC

Reputation: 80279

Maybe you can create subplots via matplotlib and fill them with the histograms?

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import seaborn as sns

tips = sns.load_dataset("tips")
columns = ['sex', 'smoker', 'day', 'time', 'size']
sns.set()
fig, axs = plt.subplots(ncols=len(columns), figsize=(15, 5))
for ax, column in zip(axs, columns):
    sns.histplot(tips[column], discrete=True, ax=ax)
    ax.xaxis.set_major_locator(MultipleLocator(1))  # set ticks at each position
    if ax != axs[0]:
        ax.set_ylabel('')
plt.tight_layout()
plt.show()

histograms of 5 columns

Upvotes: 1

Related Questions