The Startup
Published in

The Startup

Python Seaborn Tutorial

A guide on how to plot statistical graphs.

Photo by Campaign Creators on Unsplash

Before analyzing the data, you can plot to understand the data. Usually, Matplotlib or Seaborn libraries are used to visualize data in Python. In this post, I’ll talk about Seaborn library.

Matplotlib is an important library for visualizing data. But you can’t plot advanced graphics using this library alone. Matplotlib library has some shortcomings. Matplotlib has low-level APIs and requires more code for advanced graphics. Another problem of Matplotlib emerged about 10 years before Pandas, so it was not designed to use DataFrame, an important data structure of Pandas.

Because of these problems, Seaborn library was developed. This library is built on Matplotlib and allows for easier visualization of graphs.

In summary, I’ll cover the following topics in this post.

  • Seaborn vs Matplotlib
  • What is Seaborn?
  • Histogram & density plots
  • Two-dimensional plots
  • Facet grid plots
  • Box plots
  • Bar plots

Please don’t forget to subscribe to my youtube channel where I create content about AI, data science, machine learning, and deep learning. 👇

Let’s get started.

Seaborn vs Matplotlib

Now, let’s compare the two libraries by drawing graphics. First, let me import the libraries.

Let’s use classic type as graphic style.

Let’s use % matplotlib inline to see the graphics between the lines.

I’m going to generate data to compare Matplotlib and Seaborn. First, let me create an rng object.

Now, let’s generate 250 values for the x-axis with equal intervals between 0 and 10.

Let me assign random values to the rng object and use the cumsum method to add the rows of this array.

Now, let’s plot this data with Matplotlib and also add a legend.

Here you go. Now, let’s plot the same data with Seaborn. First, I’m going to import the Seaborn.

Let me call the set method to plot the Seaborn style graphics.

Let’s reuse the codes I used.

As you can see, isn’t the graph drawn with Seaborn more readable?

What is Seaborn?

Seaborn is a Python library for data visualization built on Matplotlib. Matplotlib is used to plot 2D and 3D graphs, while Seaborn is used to plot statistical graphs. Seaborn’s main goal is to easily draw graphs used for statistical data analysis. To show this, let’s load the iris dataset with the load_dataset method in Seaborn.

Let’s take a look at the first five rows of the dataset.

In the dataset, there are three species of the iris flower, and the sepal and also petal height and width of each species are shown. Let’s select the setosa and virginica species in this dataset.

Histogram & Density Plots

You can use the histogram plot to see the distribution of one or more variables Let’s plot the histogram graph of setosa’s sepal_length variable.

You can draw the density plot with the kdeplot method.

Let’s see the histogram and density plot of sepal_length along with the histplot method.

Now, let’s see the plot of sepal width and length together.

Two-Dimensional Plots

With Seaborn, you can draw great two-dimensional graphics. Let’s show this.

You can use the shade = True to add a shadow to the plot

You can use the jointplot method to see joint and marginal distributions

You can change the type of plot.

You can use the pairplot method to see the binary relationships of the variables.

Facet Grid Plots

Facet grid plots help in visualizing the distribution of one variable as well as the relationship between multiple variables separately within subsets of your dataset using multiple panels. First, I’m going to plot Faceted histograms. To show this, let’s load the tips dataset in Seaborn.

Let’s take a look at the first five rows of the dataset.

Now, let’s find the tips percentage.

You can use the FacetGrid method to see the histogram of the categories.

Let’s map this variable now.

Box Plots

The box plot is used to compare the distribution of numerical data between levels of a categorical variable. Let me show you how to draw the box plot for categorical variables.

You can use the jointplot method to see the graph of the regression line with the combined histogram and density graph of the total calculation and tip numeric variables.

Bar Plots

A bar plot represents an estimate of the central tendency for a numeric variable with the height of each rectangle. Let’s take a look at the bar plot of the sizes in the dataset.

That’s it. In this post, I talked about Seaborn library. I hope you enjoy this post. Thanks for reading. You can find the notebook here.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store