How to Create a Seaborn Palette That Highlights Maximum Value
By: Samantha Knee
Tags: Data Visualization, Python, Colors, Seaborn

While working on my latest data science project, I realized I needed a way to quickly highlight the maximum value in each of my visualizations, so my reader could focus on the conclusion to each of my analyses. I was using seaborn plots in my project, drawn to their simplicity and beautiful built-in format, and thought there must be an easy way to add a special color palette: one color for the maximum value, and one color for all other values. When my search came up empty-handed, I realized I could use what I had learned about Python functions to write my own function. This would simplify the formatting process throughout my notebook and could be replicated in projects to come. Here is a step-by-step guide to creating a maximum-highlighting palette — I hope it will be useful to you as you put together your own seaborn bar plots.

Step 1: Import Necessary Libraries



import matplotlib.pyplot as plt

import seaborn as sns

The only packages we require for this example are matplotlib and seaborn, as we can use built-in datasets from seaborn to demonstrate the power of this function.

Step 2: Load Dummy Dataset and Create Grouped DataFrame

iris_df = sns.load_dataset('iris')



grouped_iris_df = iris_df.groupby('species').mean().reset_index()

Seaborn comes with a number of built-in datasets — for this example, we will work with the Iris Flower Dataset. Demonstrating this seaborn function works best working with a grouped DataFrame, since the function will automatically highlight the highest group in the set as a separate color. In this example, I grouped the iris dataset by the mean values for each species. We cannot forget to reset the index after grouping, otherwise our graph will not plot properly.

Step 3: Decide on Default Colors

Matplotlib list of colors

The image above shows a list of some of the colors you can use in your visualization. I would recommend writing the function so that there is a default color for the maximum value we want highlighted and a color for all other values; you can always pass in different colors if you so choose, but using defaults will save you time and errors if you forget to list a color each time you use the function. I would suggest using a muted color, such as ‘lightgrey’, as the color for the bars you are not highlighting, so that the one colored bar stands out as much as possible.

Step 4: Write the Function

def set_custom_palette(series, max_color = 'turquoise', other_color = 'lightgrey'):

max_val = series.max()

pal = []



for item in series:

if item == max_val:

pal.append(max_color)

else:

pal.append(other_color)

return pal

In the above function, we are creating a list of strings that correspond to the matplotlib colors, that will be input into the palette parameter of our seaborn plot.

Let’s explain the break down of each part of the function.

def set_custom_palette(series, max_color = 'turquoise', other_color = 'lightgrey'):

The function is called set_custom_palette, an apt name, and requires a series parameter in order to run. You should pass the series being used as your dependent variable in the visualization, since it is the maximum of this series that we want highlighted. In our example this will be the ‘sepal_length’ column. We also set our default color for the maximum color as turquoise and all other colors as light grey.

max_val = series.max()

pal = []

This part of the function defines the maximum value of the series as max_val. We also created an empty list called pal, which will list out our color palette in the correct order after the function is run.

for item in series:

if item == max_val:

pal.append(max_color)

else:

pal.append(other_color)

return pal

This for loop iterates through each item in the DataFrame series being used as our dependent variable. If the item is equal to the maximum in the series, the highlighted or ‘max_color’ will be appended to the empty pal list. Otherwise, the other color, in our case light grey, will be appended. When this function is called, the pal list created will color each item in our bar plot as the correct corresponding color. Calling set_customer_palette on the grouped_iris_df[‘sepal_length’] will return a list of colors, and this list is what passes into the palette parameter of the seaborn box plot.

Step 5: Test the Function and View the Seaborn Plot

plt.figure(figsize=(12,9))

ax = sns.barplot(x = 'sepal_length', y = 'species',

data=grouped_iris_df,

palette=set_custom_palette(grouped_iris_df['sepal_length']))

ax.set_title('Average Sepal Length by Species')

ax.set(xlabel='Sepal Length', ylabel= 'Species');

And there you have it — a beautiful Seaborn plot that automatically “highlights” what I want the viewer to focus on: the maximum value of the dataset, which is the virginica average sepal length. You can then use this format throughout the rest of your visualizations so you can have a coherent looking presentation. As my next project, I am going to explore writing a function that can further automate this bar plot, to save even more time when creating identical, aesthetically pleasing plots.