Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Plotting With Seaborn

Give Feedback

Let’s bring one more Python package into the mix. Seaborn has a displot() function that plots the histogram and KDE for a univariate distribution in one step. Let’s use the NumPy array d from ealier:

import seaborn as sns

sns.set_style('darkgrid')
sns.distplot(d)
Seaborn's distplot

The call above produces a KDE. There is also optionality to fit a specific distribution to the data. This is different than a KDE and consists of parameter estimation for generic data and a specified distribution name:

sns.distplot(d, fit=stats.laplace, kde=False)
Histogram with fitted laplace distribution

Again, note the slight difference. In the first case, you’re estimating some unknown PDF. In the second, you’re taking a known distribution and finding what parameters best describe it given the empirical data.

00:00 Now that you know how to plot your own histograms and KDEs, it’s time to learn how to use Seaborn. Seaborn is a popular library that makes very nice graphs in very few lines of code. In this video, you’re going to see how quickly you can produce a histogram chart with a KDE using the NumPy dataset from earlier. If you deleted that, you can go ahead and create it again like so.

00:26 Just grab np.random and we’ll set a seed of 444. And then make an array called d that’ll just be equal to np.random.laplace(), set the loc (location) equal to 15, scale equal to 3, and the size equal to 500, like so. To use Seaborn, import seaborn as sns, and set the style to 'darkgrid'.

00:58 So you can just say sns.set_style() and then pass in the string 'darkgrid'. And to make your histogram with a KDE, you can just say sns and then call distplot() off of that, and pass in d.

01:18 Because this is being run from a script and Seaborn is a wrapper for Matplotlib, I’m just going to throw in plt.show() down here so that it doesn’t close itself out after I run the script. Save that, and run it.

01:41 And look at that! You’ve got a histogram here. It’s got its KDE overlaid on top of it. It has a very nice color scheme. And if you look, it was really just one line of code that did this.

01:53 Seaborn knows, when you take your data, kind of what you want to do with it, and you can generate pretty simple graphs very quickly. There are a ton of options you can use with Seaborn, so if you’re interested in using this library more, take a look at their documentation.

02:10 They have a great website showing all the different types of charts you can make, and you can just about find something for anything you need to do. Before we leave this one, though, this dataset was made with a Laplace distribution, and you can see that the kernel density estimate is kind of, well, lumpy.

02:28 It would be nice knowing the distribution of the underlying data if you could go ahead and fit that. So go back to Seaborn,

02:38 and in here, you can actually pass in a fit and set this equal to stats.laplace. And because this is no longer a KDE, set that equal to False.

02:52 And I’m going to have to close this out before I rerun it.

03:00 And if you get 'stats' is not defined, that makes sense, because it’s in another library called SciPy. So from scipy import stats.

03:15 And go ahead and rerun that. And if you look at that, you can see this estimate seems to fit the data much better than the other one, which makes sense because you know where the data came from.

03:29 So, that’s an example of an option you have. I don’t use these too often, going away from the regular KDEs, but it’s nice to know that you’re able to and with not much extra code needed.

03:41 All right! That’s it for the graphing part of this series. In the next video, you’re going to take a look at some tools you can use in Pandas to give yourself a little more control over your histograms. Thanks for watching.

Michal on Dec. 9, 2020

Using distplot now raises FutureWarning: distplot is a deprecated function and will be removed in a future version. Please adapt your code to use either displot (a figure-level function with similar flexibility) or histplot (an axes-level function for histograms).

(seaborn 0.11.0)

However, neither displot nor histplot accept the fit argument.

Become a Member to join the conversation.