Data Visualization with Python (12): Seaborn

Sawsan Yusuf
4 min readJun 18, 2023

--

There are many ways to visualize data. And as I mentioned in the previous article, we use four libraries for making visualizations: pandas, Matplotlib, plotly express, and Seaborn. In the past articles, we learned about Matplotlib and Plotly Express. In this article, we’ll focus on using Seaborn. So, what is Seaborn?

Seaborn is a library primarily used for statistical plotting in Python. It is built on top of Matplotlib and provides beautiful default styles and color palettes to make statistical plots more attractive.

We can import the Seaborn library with the allies “sns” in this way:

import seaborn as sns

First of all, let’s look at the data we will use in this article.

Dataset: Automobile Prices

We will be using the car prices dataset from UC Irvine Machine Learning Repository.

# Import required libraries
import pandas as pd
import seaborn as sns

# Read the airline data into pandas dataframe
df = pd.read_csv('carprices_data.csv')

# Preview the first 5 lines of the loaded data
df.head()
# Shape of the data
df.shape


(201, 29)

Visualization with Seaborn

1. Scatter Plots

  • Learn more about the usage of scatter plots here.

We can plot a Scatter plot with Seaborn using the scatterplot() method.

Syntax:

seaborn.scatterplot(x=None, y=None, data=None, **kwargs)

Example:

sns.scatterplot(x="highway-mpg", y="price", data=df);

2. Line Plot

  • Learn more about the line plot here.

To obtain this plot with Seaborn we use a function called lineplot().

Syntax:

seaborn.lineplot(x=None, y=None, data=None, **kwargs)

Example:

sns.lineplot(x="body-style", y="price", data=df);

3. Bar Chart

A bar plot represents an estimate of the central tendency for a numeric variable with the height of each rectangle.

  • Learn more about the bar chart here.

To use Seaborn to make a bar chart we use the function sns.barplot() in this syntax:

Syntax:

barplot([x, y, hue, data, order, hue_order, …])

Example:

sns.barplot(x="body-style", y="price", data=df);

4. Histogram

  • Learn more about histograms here.

To make our histogram, we will use sns.histplot().

Syntax:

seaborn.histplot(data=None, *, x=None, y=None, hue=None, **kwargs)

Example:

sns.histplot(x='price', data=df, bins=8);

5. Distplot

Distplot is used for a univariant set of observations and visualizes it through a histogram. It is potted using the distplot() method.

Syntax:

distplot(a[, bins, hist, kde, rug, fit, …])

Example:

sns.displot(df['price'], bins=8, kde=True);

6. Count Plot

A countplot counts the categories and returns a count of their occurrences. It is one of the most simple plots provided by the seaborn library. We can create it using the countplot() method.

Syntax:

countplot([x, y, hue, data, order, …])

Example:

sns.countplot(x="body-style",data=df);

7. Box Plot

  • Learn more about Bar plots here.

Syntax:

boxplot([x, y, hue, data, order, hue_order, …])

Example:

sns.boxplot(x="body-style", y="price", data=df);

8. Heatmap

Heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix. In this, to represent more common values or higher activities brighter colors and reddish colors are used, and to represent less common or activity values, darker colors are preferred. it can be plotted using the heatmap() function.

Syntax:

seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, **kwargs)

Example:

df_corr = df.corr()
sns.heatmap(df_corr, cmap=sns.diverging_palette(220, 20, n=12), annot=False)

Conclusion

In this article, we learned how to use Seaborn for creating some plots and charts. And with this, we finished our “Data Visualization with Python” series. I hope I have provided you with valuable information on your learning journey. See you in another series.

Until that, stay well.

Thank you very much for reading.
Sawasn

--

--

Sawsan Yusuf
Sawsan Yusuf

Written by Sawsan Yusuf

Data Scientist | Mechatronics Engineer

No responses yet