Seaborn — Let’s make plotting fun
By: Karan Bhanot
Tags: Machine Learning, Technology, Productivity, Data Science, Data Visualization

Seaborn

Let’s start by importing matplotlib . Note that I’ve used matplotlib version 3.0.3 and not the latest version as there is a bug which breaks the heatmap and renders it useless. Then, I imported seaborn . Lastly, to make sure the plots in Jupyter show inside the notebook, we use the command %matplotlib inline .

Let’s start exploring the plots!

Scatter Plot

The scatter plot is useful when we want to show the relation between two features or a feature and the label. It is useful as we can also describe the size of each data point, color them differently and use different markers. Let’s see what the basic command in seaborn does.

Scatter plot in seaborn

The plot above describes the relation between total_rooms and total_bedrooms . Just one command did all that but wait, there is more.

Using figsize, I increased the size to 12x8. Then, I updated the scatterplot command with size of each data point based on median_house_value , color using hue based on ocean_proximity and markers using style based on ocean_proximity . Also, a plot is incomplete without a proper title and axis labels, so I added them as well.

Updated scatter plot using seaborn

As you can see, this plot looks so much better than the previous one and also includes a nice legend so anyone can see and understand the plot — the way it is meant to be.

Count Plot

Count plots automatically count the data points based on a certain categorical column and present the data as a bar plot. This is exceptionally useful in classification problems where we want to see if the various classes are of same size. However, as this is not a classification data and there is only one categorical column, I decided to go with it.

The plots in seaborn allow us to also add text to each bar using annotate . On taking a closer look at the dataset, we see that a lot of metadata information is missing. For example, the column ocean_proximity has the value <1H OCEAN which has not been described anywhere. One should always collect metadata information as well as work with datasets which have proper information. As this is just a reference dataset for understanding plots, it’s not that big of a deal.

Count plot using seaborn

In the plot above, we can see that the data for this column is highly skewed. Having text along with bars is really useful as the last type ISLAND looks like a zero value just by looking at the plots.

Histograms

Histograms are an effective way to show continuous points of data and see how they are distributed. We could see that most values are to the lower side, or to the higher side or evenly distributed.

The dist plot in seaborn produces both a histogram as well as a density line on that plot based on the data. I defined a total number of 10 bins such that the whole median_house_value is distributed into 10 different buckets.

Histogram (with density) in seaborn

As we can see, the distribution seems quite normal with a slight spike at the higher side. The blue line in the plot above defines the distribution of density.

Violin Plot

Before working with seaborn, I always saw these weird looking plots in various articles and wondered what they were. Then, I read about them and found out that they were violin plots which are quite similar to box plots and depict the width based on the density to reflect the data distribution. Creating a violin plot is just a command away in seaborn.

Violin plot in seaborn

Before we move ahead, let’s see how to understand these plots. Consider the green plot INLAND . The black line that extends from zero to approximately 250000 is the 95% confidence interval. The thick black block inside is the interquartile range meaning approximately 50% of all data lies in this range. The width of the plot is based on the density of the data. We can understand it as the histogram of this specific dataset with the black line as the x-axis that is completely smoothed out and turned 90 degrees.

Heatmap

A correlation matrix helps us see how all features and labels are related to one another and the level of dependance. The pandas dataframe has a function called corr() which generates a correlation matrix and when we input it to the seaborn heatmap, we get a beautiful heatmap. Setting annot as True ensures that the correlations are also defined with numbers.

Heatmap in seaborn

While the whole plot is useful, we can start by taking a look at the last column and noticing how each feature may be related to the label median_house_value . median_income is the most related to the label with a value of 0.69 .

Joint Plot

A joint plot is a combination of scatter plot along with the density plots (histograms) for both features we’re trying to plot. The seaborn’s joint plot allows us to even plot a linear regression all by itself using kind as reg . I defined the square dimensions using height as 8 and color as green .

Joint plot in seaborn

The green line depicts the linear regression based on the data points.

Box Plot with Swarm Plots

The box plots present the information into separate quartiles as well as the median. When overlapped with swarm plot, the data points are spread across their location such that there is no overlapping at all.

Box plot (and swarm plot) in seaborn

From the blot above, we can see how the box plots are described for each of the five categories in ocean_proximity . The data points reveal how the data is spread across.

Pair Plot

The pair plot results in a large collection of plots between every pair of feature and label. For each combination of feature/label, this plot shows a scatter plot and for each combination with itself, it shows a histogram. The plot in itself is really useful to get a good essence of the data at hand.

Pait plot in seaborn

The plot above includes an enormous amount of information and that too with just a single command.

Conclusion

In this article, we explored a few of the infinite seaborn plots that can be made to better understand how the library works and what it is capable of. Keep practicing and experimenting as the options are almost limitless.