• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Seaborn / Boxplot with Seaborn / How To Specify Colors to Boxplots in Seaborn?

How To Specify Colors to Boxplots in Seaborn?

November 19, 2018 by cmdlinetips

Boxplots with Specific Colors
Boxplots with Specific Colors

Boxplots with actual data points are one of the best ways to visualize the distribution of multiple variables at the same time. Creating a beautiful plot with Boxplots in Python Pandas is very easy. In an earlier post, we saw a good example of how to create publication quality boxplots with Pandas and Seaborn. If you haven’t heard of Seaborn,

Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics

Often you may want to visualize multiple variables as boxplot such that each group has specific color, not the “palette” options available in Seaborn.

Let us see an example of how to make boxplot suing Seaborn such that we use specific color for each box.

Let us first load the packages needed.

import pandas as pd
# import matplotlin
import matplotlib.pyplot as plt
# import seaborn
import seaborn as sns
%matplotlib inline

Let us load the gapminder data from software carpentry website and subset the data to make it a smaller dataframe. Now the data frame contains rows corresponding to the year 2007.

gapminder_2007 = gapminder[gapminder['year']==2007]
gapminder_2007.head(n=3)

        country  year         pop continent  lifeExp    gdpPercap
11  Afghanistan  2007  31889923.0      Asia   43.828   974.580338
23      Albania  2007   3600523.0    Europe   76.423  5937.029526
35      Algeria  2007  33333216.0    Africa   72.301  6223.367465

Let us say want to make a boxplot visualizing distributions of lifeExp variable across the continents from the gapminder data. Let us say we also want a specific color for each continent already available as Hex Code(#RRGGBB).

continents = gapminder_2007.continent.unique().tolist()
# Hex code for each continents color
continent_colors=["#F0F000","#F00000","#00A000","#00A0F0","#1010F0"]


Let us create a color dictionary with continent as key and its color as value

color_dict = dict(zip(continents, continent_colors))

Let us make basic boxplot using Seaborn’s boxplot function with liefExp on y-axis and continent on x-axis with default colors available in Seaborn.

bplot=sns.boxplot(y='lifeExp', x='continent', 
                 data=gapminder_2007, 
                 width=0.5)

This boxplot has default colors specified by Seaborn and we want to change that.

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

Now let us fill each box with the specified color using artists and set_facecolor functions. If you want to know more about Artist objects, read this fantastic blogpost.

for i in range(0,5):
    mybox = bplot.artists[i]
    mybox.set_facecolor(color_dict[continents[i]])

Now let us add the data points on top of the boxplot in black color using Seaborn’s stripplot.

bplot = sns.stripplot(y='lifeExp', x='continent', 
                      data=gapminder_2007,
                      jitter=True, marker='o',
                      alpha=0.8, 
                      color="black")
How To Make Boxplots with Specific Colors for each box with Seaborn
How To Make Boxplots with Specific Colors for each box with Seaborn

One can also specify colors with their names instead of Hexcodes. Here is an example using color names to specify box colors of boxplots.

continent_colors=["tomato","darkturquoise","mediumpurple","springgreen","magenta"]

Here is the corresponding boxplot, but this time plotting distributions of gdpPercap across the five continents as boxplots colored by using color names.

Boxplot with Colors Specified with Color names in Seaborn
Boxplot with Colors Specified with Color names in Seaborn

Here are two resources for learning color names in Python.

  1. https://matplotlib.org/users/colors.html
  2. https://matplotlib.org/examples/color/named_colors.html

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Plot Boxplot and swarmplot in Python with SeabornHow to Make Boxplots in Python with Pandas and Seaborn? Grouped boxplot seabornHow To Make Grouped Boxplots in Python with Seaborn? How To Specify Colors to Scatter Plots in Python Customizing Grouped BoxplotHow To Make Grouped Boxplots with ggplot2?

Filed Under: Boxplot with Seaborn, Python Boxplot, Python Tips, Seaborn Boxplot, Specify Colors in Boxplot Tagged With: Python Boxplot, Seaborn Boxplot, Specify Colors in Boxplot

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version