Using matplotlib to create a custom seaborn jointplot
Photo by Isaac Smith on Unsplash

Matplotlib is a comprehensive and vast library for creating visualizations in Python. It offers many tools to visualize data in a clear manner. Yet, there are some other libraries that will help you to “upgrade” your charts to a new level, letting them to be more awesome from what they already are. One of this libraries is seaborn. Often people use them together on their daily basis analysis. The reason being is that seaborn works on Matplotlib.

One of the coolest plots I have found in seaborn is the jointplot. According to seaborn documentation, this method allows you to draw a plot of two variables with bivariate and univariate graphs. In other words, it creates a scatter plot within a grid which will show you both variable’s distributions as shown as follows:

Plot created By Edgar Avalos Gauna

For the previous plot, heights and weigths data set from Kaggle was used. This dataset is really useful and simple. It comprisses of 10,000 datapoints, each one with 3 different features: Gender, Height, and Weight. There are some interesting analysis that can be performed on this dataset as shown here. Yet, when using the same dataset on the jointplot, it is not possible to see a difference between men and women. For instance, if we want to do a deeper analysis on weights, the jointplot is not as useful as other tools might be. After going through the jointplot documentation, I haven´t found any argument that will help me on this task. Therefore I decided to create my own jointplot from scratch, using Matplotlib subplots as base grid.

First thing is, we need to replicate each plot from the jointplot. Then each plot will be ensemble together using Matplotlib subplots. Finally some tuning aspects will be introduced to make our plot prettier. Let’s start by importing our data:

After importing our data, the next step is to create each plot from the joint plot. In order to do it, each plot must show 2 sets of data, one for men and one for women. For the scatter plot, I used the kdeplot function from seaborn. The code looked like this:

So far so good. The next step is create the plot distribution for height and weight. Let’s start with heights as it will go on top of the scatter plot:

Similarly, we will create weight’s distribution. Altough, for this plot, it is importand to rotate it 90 degrees to be similar to what we get from the joint plot. This can be done really easy with the addition of the vertical argument to the code of the distplot:

Finally, let us use the subplots function from Matplotlib to create a 2 by 2 grid. Additionaly, the off option will allow us to remove the upper right plot axis:

Now let´s put them all together. I added some other options so the final result looks much nicer.

And that’s it. I hope this will help you to improve your plots and your seaborn skills.
['Edgar Avalos Gauna']
['Python3', 'Matplotlib', 'Jointplot', 'Seaborn', 'Subplot']