Python

Data Visualization with Seaborn

In this post on Data Visualization with Seaborn, I will explain an important library of python language known as Seaborn. Even though we can use pandas for data visualization, but Seaborn offers more features. Actually, the graphs or charts that we can draw using seaborn are much attractive and informative. Further, we can visualize the distribution of data, regression model, visualize statistical relationships, and plot the categorical data using the seaborn package.

Data Visualization with Seaborn
Data Visualization with Seaborn

Installing Seaborn Package

If python is already installed on your machine, you can install Seaborn by running the following command. In fact, this package also dependency on NumPy, scipy, matplotlib, and pandas. Therefore all these packages will also be installed if they are not already there.

pip3 install Seaborn

After installing seaborn, you can confirm its installation by running following command on the python prompt as shown below.

Installing Seaborn
Installing Seaborn

In order to visualize the data, we need to load our dataset. In fact, the load_dataset() method can load any of the available datasets from the online repository. However, we can also use our own CSV file. The following code shows examples of both.

Loading Dataset from the Online Repository

The following code loads a dataset named exercises from the online repository and prints top five rows.

import seaborn as sb
p1=sb.get_dataset_names()
print(p1)

df1=sb.load_dataset("exercise")
print(df1.head())

Output

Using load_dataset() method of the Seaborn Package
Using load_dataset() method of the Seaborn Package

Loading a CSV File as dataset

Similarly, we can load a CSV file also that we want to use as a dataset. The following code shows that we use the pandas package for this purpose. Since the load_csv() function of the pandas package returns a data frame, we can use this data frame for visualizing data with the seaborn package.


import pandas as pd

df1=pd.read_csv("temperature_humidity.csv")
print(df1.head())

Output

Loading a CSV File for a dataset
Loading a CSV File for a dataset

As an illustration of Data Visualization with Seaborn, let us draw few plots using our dataset of Temperature and humidity values. Actually, this data was collected using an Arduino-based temperature and humidity sensor (DHT11). First of all, we create a bar plot as shown in the following code.

import seaborn as sb
sb.barplot(x = 'Temperature', y = 'Humidity', data = df1,
            palette = 'hot',

            capsize = 0.05,
            saturation = 8,
            errcolor = 'gray', errwidth = 2,
            ci = 'sd'
            )
plt.show()
Bar Plot using Seaborn
Bar Plot using Seaborn

The following example demonstrates a Count Plot.

import seaborn as sb
sb.countplot(x = 'Temperature', hue='Humidity', data = df1, palette = 'gist_rainbow')
plt.title('Activity')
plt.show()
Count Plot using Seaborn
Count Plot using Seaborn

Another useful plot in seaborn package is the Point Plot. The Point Plot measuring the central tendency is shown below.

import seaborn as sb
sb.pointplot(x='Temperature', y='Humidity', data=df1)
plt.title('Activity')
plt.show()
Point Plot Using Seaborn

In general, we show multiple data at several units using the violin plot. The Violin Plot for Temperature variable is shown below.

import seaborn as sb
sb.violinplot(x='Temperature', data=df1, palette='hot')
plt.title('Activity')
plt.legend()
plt.show()
Violin Plot for Temperature
Violin Plot for Temperature

In similar fashion the Violin Plot for Humidity variable is also here.

import seaborn as sb
sb.violinplot(x='Humidity', data=df1, palette='magma')
plt.title('Activity')
plt.legend()
plt.show()
Violin Plot for Humidity
Violin Plot for Humidity

Summary

In conclusion, Data Visualization with Seaborn provides us sufficient tools to know important information contained in the data visually. as can be seen, we can have numerous methods to draw plots for the numerical data as well as the categorical data. Some of these plots are the bar plot, histogram, joint plot, point plot, box plot, and so on.


Related Topics

A Brief Introduction of Pandas Library in Python

Object-Oriented Programming with Python

The Python Libraries that you should Learn for Analyzing IoT Data

A Brief Tutorial on NumPy in Python

Data Visualization with Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *