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