Previously, we learned to draw a scatter plot with Seaborn and to add a third dimension or variable for comparison using the hues and styles. Sometimes we might need to visualise more dimensions, but in such cases we typically end up having so much data that a scatter plot becomes too messy to be meaningful.
For example, refer to the link here where an expert data analyst has tried to explain why overplotting can cause unnecessary confusion for the audience and how you can avoid that. Think about how you can apply these tips to your own plots.
Let us take the example of a penguin data set that contains penguins’ bill length, bill depth, and body mass, segregated by species, the island where they live, and sex.
To make complex comparisons easier, Seaborn provides a function called relplot, which is short for relationship plot. relplot takes some of the same arguments as scatterplot, such as data, x, y, and hue, but adds other arguments as well.
The major difference between relplot and scatterplot (as well as others) is that it can also segregate data attributes into rows and columns of subplots. To do this, you will use the col or row keyword argument. Either of these arguments will take the name of a DataFrame column (series), and then create a new column or row (respectively) containing plots, for each different value in that series.
Demonstration
Step 1
First, import the Pandas and Seaborn libraries.
importpandasaspd importseabornassns
Step 2
Next, read the penguin.csv file extracted from the zipped folder previously to load the data and read the index of the DataFrame.
Let us redraw the plot using relplot, and also make use of the col argument. We’ll split up the species into separate columns, and then vary the hue to change the colour of points based on the penguin’s sex.
Notice that Seaborn uses the same axes scales throughout the plots. This allows us to visually compare the species. Even though they are on different plots, we can tell that Adelie penguins are smaller than Chinstrap, which in turn are smaller than Gentoo. These colours allow us to see that female penguins are generally smaller than males.
Step 4
Let’s have a quick look at the row keyword argument too. We can use it in place of col to split attributes into separate rows instead of columns. Alternatively, we can use it with col to visualise yet another variable. Let’s use it in our relationship plot so show the different islands on each row.
This plot quickly shows us something that was not immediately obvious before – the type of species that live on respective islands. Adelie penguins are found on all three types of islands, whereas Chinstrap are found only on Dream, and Gentoo on Biscoe.
For more customisation options for the relplot function, refer to the link provided here. Read through the options to improve your understanding.