Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram bin size in seaborn

I'm using Seaborn's FacetGrid to plot some histograms, and I think the automatic bin sizing uses just the data of each category (rather than each subplot), which leads to some weird results (see skinny green bins in y = 2):

g = sns.FacetGrid(df, row='y', hue='category', size=3, aspect=2, sharex='none')
_ = g.map(plt.hist, 'x', alpha=0.6)

Seaborn histogram

Is there a way (using Seaborn, not falling back to matplotlib) to make the histogram bin sizes equal for each plot?

I know I can specify all the bin widths manually, but that forces all the histograms to be the same x range (see notebook).

Notebook: https://gist.github.com/alexlouden/42b5983f5106ec92c092f8a2697847e6

like image 999
Alex L Avatar asked Feb 16 '17 04:02

Alex L


People also ask

How do you set bins in Seaborn histogram?

bins. The bins parameter enables you to control the bins of the histogram (i.e., the number of bars). The most common way to do this is to set the number of bins by providing an integer as the argument to the parameter. For example, if you set bins = 30 , the function will create a histogram with 30 bars (i.e., bins).

What is bins in histogram Seaborn?

Histograms represent the data distribution by forming bins along the range of the data and then drawing bars to show the number of observations that fall in each bin. Seaborn comes with some datasets and we have used few datasets in our previous chapters.

Does Seaborn have Histplot?

Seaborn enables us to plot both the histogram bars as well as a density curve obtained the same way than kdeplots. With Seaborn, histograms are made using the histplot function. You can call the function with default values, what already gives a nice chart.


1 Answers

You'll need to define a wrapper function for plt.hist that does the hue grouping itself, something like

%matplotlib inline

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
tips.loc[tips.time == "Lunch", "total_bill"] *= 2

def multihist(x, hue, n_bins=10, color=None, **kws):
    bins = np.linspace(x.min(), x.max(), n_bins)
    for _, x_i in x.groupby(hue):
        plt.hist(x_i, bins, **kws)

g = sns.FacetGrid(tips, row="time", sharex=False)
g.map(multihist, "total_bill", "smoker", alpha=.5, edgecolor="w")

enter image description here

like image 148
mwaskom Avatar answered Oct 23 '22 20:10

mwaskom