• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

Coloring Boxplots with Seaborn Palettes

datavizpyr · December 18, 2019 ·

Boxplots made with Seaborn automatically gets colors by default. Sometimes you might want to change and assign different colors to the boxes in the boxplot. For example, one might want to color boxplots with colors that are colorblind friendly.

One great solution to color the boxes in boxplot is to use many of the available color palettes from Seaborn. Note these color palettes general for any visualization with Seaborn, not just for boxplots.

In this post, we will see examples of using Seaborn’s color palettes for coloring boxplots in two ways.

Let us load the necessary packages.

 
import seaborn as sns 
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

We will generate some data to make the boxplots.

 
np.random.seed(42)
# Generating Data
df = pd.DataFrame({
    'Africa': np.random.normal(40, 15, 100),
    'Asia': np.random.normal(60, 10,100),
    'Americas': np.random.normal(80, 5, 100)
})
print(df.head())

The data we generated is in wide form. Let us convert the data in wide form to long form using Pandas’ melt function.

 
data_df = df.melt(var_name='continent',value_name='lifeExp')
print(data_df.head())

Let us first make basic boxplot with Seaborn’s boxplot() function.

 
sns.boxplot(x="continent", y="lifeExp", data=data_df)

We can see that the boxes in boxplots are filled with default colors.

Boxplot with default colors in Seaborn
Boxplot with default colors in Seaborn

There are two ways to set colors for boxes in boxplot. The first way is set the Seaborn color palette we want using the palette argument inside the boxplot() function. In this example below, we specify the color palette using the argument palette=”Set1″.

 
sns.boxplot(x="continent", y="lifeExp", data=data_df, palette="Set1")

We see that the boxes are colored by the colors chosen by the color palette “Set1”.

Boxplot with palette="Set1" within Boxplot function
Seaborn: palette=”Set1″ within Boxplot function

Similarly we can use any of the available Seaborn color palettes as argument to palette inside the bioxplot function,

 
sns.boxplot(x="continent", y="lifeExp", data=data_df, palette="Set3")

Here we have used paletter=”Set3″.

Seaborn: palette="Set3" within Boxplot function
Seaborn: palette=”Set3″ within Boxplot function

Another way to use the Seaborn color palettes and set colors for boxplot is to use the function set_palette() outside the boxplot() function. As you can see, the advantage is that the set_palette() functions allows us choose a color palette for all the plots in the session not just the boxplot.

 
sns.set_palette("Reds")
sns.boxplot(x="continent", y="lifeExp", data=data_df)

In this example, we have used the color palette “Reds” and set it using set_palette() function.

Seaborn: Colors with set_palette()
Seaborn: Colors with set_palette() function

We can also create our own color palette. For example, we can manually select a list of colors and use them as our color palette.

 
my_colors = ["#9b59b6", "#3498db", "#2ecc71"]
sns.set_palette(my_colors)
sns.boxplot(x="continent", y="lifeExp", data=data_df)

Here is the boxplot using manually defining colors for color palette and using them with set_palette() function.

Seaborn manual colors with set_palette()
Seaborn manual colors with set_palette()

Related posts:

Boxplot with Catplot Seaborn PythonHow To Make Boxplots with Seaborn in Python? How To Manually Order Boxplot with Seaborn?How To Manually Order Boxplot in Seaborn? Grouped Boxplot with Seaborn CatplotHow To Make Grouped Boxplot with Seaborn Catplot? Grouped Boxplot with Jittered Data Points SeabornGrouped Boxplot with Jittered Points with Seaborn Python

Filed Under: Python, Seaborn Boxplot, Seaborn Color palette Tagged With: Color Palette, Python

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version