I'm running through a tutorial to understand the histogram plotting. Given the seaborn tips dataset, by running the sns.distplot(tips.tip); function the following plot is rendered.

enter image description here

Looking at the plot, I don't understand the sense of the KDE (or density curve). The middle column (the one with the lower value) between 2 and 4 doesn't seem to support the shape of the curve.

I have to say that I have little if no understanding on the principle used to plot it, so I would love to hear from somebody more experienced on

  • What's the added value of the KDE?
  • What's the process behind the calculation

Also, why using the same dataset with the standard matplotlib I get a slightly different representation (in which the density line above probably fit better)?

enter image description here

  • $\begingroup$ Could it have something to do with the different binsizes? The standard matplotlib plot has bigger bins than the seaborn plot since it uses bins=10 whereas seaborn seems to use the Freedman-Diaconis rule to determine the number of bins. $\endgroup$ – Oxbowerce Jan 15 at 19:52
  • $\begingroup$ Can you post the data used to generate the plots? $\endgroup$ – TitoOrt Jan 16 at 9:18
  • $\begingroup$ @TitoOrt as I was saying, I am using the seaborn integrated dataset. Do run sns.load_dataset("tips") and you can get them. $\endgroup$ – Andrea Moro Jan 16 at 11:14
  • $\begingroup$ @Oxbowerce can you elaborate on this bins topic? I was trying to understand them yesterday, but the reference guide doesn't help that much to a point I was passively accepting the default. $\endgroup$ – Andrea Moro Jan 16 at 11:15
  • $\begingroup$ @AndreaMoro see the answer that I posted below. $\endgroup$ – Oxbowerce Jan 18 at 12:46

The difference is caused by the fact that seaborn.distplot and matplotlib.pyplot.hist use different defaults for the number of bins. The bins are ranges of values for which the number of observations are counted before being plotted. For more information on what bins are check the Wikipedia page for histograms.

In your example, the standard matplotlib plot has bigger bins than the seaborn plot since it uses bins=10, whereas seaborn seems uses the Freedman-Diaconis rule to determine the number of bins, which in this case would give a bin width of about 0.5 and bins=18.

Setting the number of bins used equal for both the seaborn and matplotlib.pyplot plots gives the following histograms: enter image description here

As you can see, using the same value for the number of bins gives the exact same plots. I used the following code to produce this plot, in which you can also change the number of bins used by both plots to compare them.

import seaborn as sns
import matplotlib.pyplot as plt

# Set number of bins
nbins = 10

# Load dataset 
x = sns.load_dataset("tips")

# Set up subplots
fig, axs = plt.subplots(1, 2, figsize=(12, 5))

# Seaborn plot
sns.distplot(x.tip, ax=axs[0], bins=nbins, kde=False)
axs[0].set_title("Seaborn plot")

# Matplotlib.pyplot plot
axs[1].hist(x.tip, bins=nbins)
axs[1].set_title("Matplotlib.pyplot plot")

# Set title
fig.suptitle(f"Histograms using $bins=${nbins}")

| improve this answer | |
  • $\begingroup$ Thanks for your time to explain. Glad to see it is also possible to mix graph plotted with different types together. $\endgroup$ – Andrea Moro Jan 20 at 5:16
  • $\begingroup$ Are there any property on the seaborn plot to query these variables and understand what the default would have been? I was expecting to do something like sns.distplot.bins to retrieve that, but that property is not exposed. Any idea? $\endgroup$ – Andrea Moro Jan 20 at 7:03
  • $\begingroup$ If my above explanation answers your question, please mark my comment as the answer. Regarding the default, you can simply check the documentation, in this case for seaborn, to see what the defaults are for seaborn.distplot. For the bins argument it says the following: "Specification of hist bins, or None to use Freedman-Diaconis rule.", meaning that if bins=None (which is the default) it will use the Freedman-Diaconis rule to calculate the number of bins to use. You can then go to the wiki page to find equation for this rule. $\endgroup$ – Oxbowerce Jan 20 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.