Published in

The Startup

# Python Seaborn Tutorial

## A guide on how to plot statistical graphs.

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 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.

--

--

## More from The Startup

Get smarter at building your thing. Follow to join The Startupâ€™s +8 million monthly readers & +760K followers.