A Complete Guide to Plotting Categorical Variables with Seaborn
By: Will Norris
Tags: Seaborn, Python Programming, Python, Data Visualization, Tutorial

Categorical Distribution Plots

We have two different kinds of categorical distribution plots, box plots and violin plots. These kinds of plots allow us to choose a numerical variable, like age, and plot the distribution of age for each category in a selected categorical variable.

Box Plots

Many of us have probably made quite a few box plots over the years. They are an easy and effective way to visualize groups of numerical data through their quartiles. Seaborn makes creating attractive box plots simple and allows us to easily compare an extra dimension with the hue argument that appears in many Seaborn functions.

Basic Boxplot

Lets take a look at distribution of age by passenger class.

plt.figure(figsize=(8,5)) sns.boxplot(x='class',y='age',data=titanic, palette='rainbow') plt.title("Age by Passenger Class, Titanic")

Basic Seaborn Boxplot

We can see that age tends to decrease as you go down in passenger class. That makes sense, young people tend to travel on a budget. Notice how little code this required to create a pretty aesthetically pleasing plot? Seaborn’s basic plots are very polished.

Also pay attention to how we can wrap Matplotlib formatting syntax around our Seaborn plots. This only works when we are using Axis-level functions, which you can read about in another one of my posts about figure-level and axis-level functions in Seaborn.

Adding Hue

Like many other plots available in Seaborn, box plots can take an added hue argument to add another variable for comparison.

Boxplot with Hue

Adding the hue shows us that regardless of class the age of passengers that survived was generally lower than those who passed away.

Having the hue for additional comparison allows this box plot to be quite information dense. The more complex the plot gets the longer it will take for viewers to comprehend it, but it is nice to have the option when interesting insights are more easily shown with an added dimension.

Violin Plots

Violin plots are not very frequently used but I have found them to be useful on occasion, and they are an interesting change from more popular options. They plot a vertical kernel density plot for each category and a small box plot to summarize important statistics.

plt.figure(figsize=(10,6)) sns.violinplot(x='class',y="age",data=titanic, hue='sex', palette='rainbow') plt.title("Violin Plot of Age by Class, Separated by Sex")

Violin Plot

While I like this plot, I think it is easier to compare the genders with slightly different formatting:

plt.figure(figsize=(10,6)) sns.violinplot(x='class',y="age",data=titanic, hue='sex', split='True', palette='rainbow') plt.title("Violin Plot of Age by Class, Separated by Sex")

Violin Plot Split on Hue

When we split the violin on the hue it is a lot easier to see the differences in each KDE. However, the IQR stats aren’t split by the sex anymore; instead they apply to the entire class. So there are trade-offs to styling your plot in certain ways.

Boxen Plot

The boxen plot, otherwise known as a Letter-value plot, is a box plot meant for large data sets (n > 10,000). It is similar to a traditional box plot, however it essentially just plots more quantiles. With more quantiles, we can see more info about the distribution shape beyond the central 50% of the data; this extra detail is especially present in the tails, where box plots tend to give limited information.

plt.figure(figsize=(8,5)) sns.boxenplot(x='class', y='age', data=titanic, palette='rainbow') plt.title("Distribution of Age by Passenger Class")

Simple Boxen Plot

Just in case there still isn’t enough going on here for you, we can also add a hue to a boxen plot!

plt.figure(figsize=(8,5)) sns.boxenplot(x='class', y='age', data=titanic, palette='rainbow', hue='survived') plt.title("Distribution of Age by Passenger Class, Separated by Survival")

Boxen Plot with Hue

We can see that the boxen plot gives us much more information beyond the central 50% of the data. However, keep in mind that boxen plots are meant for larger data sets with entries between 10,000 and 100,000. This data set of under 1,000 entries is definitely not ideal. Here is a link to the paper where boxen plots were created that explains them very well.