• 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 / Python Tips / How To Specify Colors to Scatter Plots in Python

How To Specify Colors to Scatter Plots in Python

April 20, 2019 by cmdlinetips

Scatter plots are extremely useful to analyze the relationship between two quantitative variables in a data set. Often datasets contain multiple quantitative and categorical variables and may be interested in relationship between two quantitative variables with respect to a third categorical variable.

And coloring scatter plots by the group/categorical variable will greatly enhance the scatter plot. In this post we will see examples of making scatter plots and coloring the data points using Seaborn in Python. We will use the combination of hue and palette to color the data points in scatter plot.

Let us first load packages we need.

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

We will use gapminder data to make scatter plots.

data_url = 'http://bit.ly/2cLzoxH'
gapminder = pd.read_csv(data_url)
print(gapminder.head(3))

gapminder data set contain data over many years. We will subset the data by filtering rows for two specific years.

gapminder=gapminder[gapminder.year.isin([2002,1962])]

Scatterplot with Seaborn Default Colors

Seaborn has a handy function named scatterplot to make scatter plots in Python. Note that one could also use other functions like regplot.

We provide the Pandas data frame and the variables for x and y argument to scatterplot function. In addition to these arguments we can use hue and specify we want to color the data points based on another grouping variable. This will produce points with different colors.

g =sns.scatterplot(x="gdpPercap", y="lifeExp",
              hue="continent",
              data=gapminder);
g.set(xscale="log");

In our example we also scale the x-axis to log scale to make it easy to see the relationship between the two variables.

Specify colors scatter plot Seaborn Python_1

Manually specifying colors as list for scatterplot with Seaborn using palette

The above scatter plot made by Seaborn looks great. However, often many times we would like to specify specific colors , not some default colors chosen by Seaborn. To color the data points with specific colors, we can use the argument palette. We can specify the colors we want as a list to the palette argument.

In our example below, we specify the colors we want a list [‘green’,’orange’,’brown’,’dodgerblue’,’red’].

g =sns.scatterplot(x="gdpPercap", y="lifeExp", hue="continent",
              data=gapminder, 
                    palette=['green','orange','brown','dodgerblue','red'], legend='full')
g.set(xscale="log")

Note that now the data points on scatter plot are colored by the colors we specified.

Manually specifying colors as a dictionary for scatterplot with Seaborn using palette

Another option to manually specify colors to scatter plots in Python is to specify color for the variable of interest using a dictionary.

In our example, we specify a color for each continent a Python dictionary.

color_dict = dict({'Africa':'brown',
                  'Asia':'green',
                  'Europe': 'orange',
                  'Oceania': 'red',
                   'Americas': 'dodgerblue'})

We can use the color dictionary for the argument palette and make scatter plots.

g = sns.scatterplot(x="gdpPercap", y="lifeExp", hue="continent",
              data=gapminder, palette=color_dict, 
                   legend='full')
g.set(xscale="log")

And we get the scatterplot colored by the colors specified in the dictionary.

These are not the only options to color the data points with Seaborn. Seaborn offers rich color palettes to color the data points. See https://seaborn.pydata.org/tutorial/color_palettes.html .

Let us choose color palette that is color blind friendly. Seaborn’s colorblind palette gives the option.

g = sns.scatterplot(x="gdpPercap", y="lifeExp", hue="continent",
              data=gapminder, palette='colorblind', 
                   legend='full')
g.set(xscale="log")

Now we have colored the data points by continent using colorblind friendly colors.


Share this:

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

Related posts:

Adjusting Transparency in Scatter PlotHow To Make Scatter Plot in Python with Seaborn? Scatter plot tips: Color & Shape by variable9 Tips to Make Better Scatter Plots with ggplot2 in R How To Highlight Data Annotate with Text Python?How to Highlight Data Points with Colors and Text in Python Boxplots with Specific ColorsHow To Specify Colors to Boxplots in Seaborn?

Filed Under: Python Tips, Scatter plot Python, Specify Colors Scatter Plot Seaborn Tagged With: Colors Seaborn Scatter Plot, scatter plot colors Python, Scatter plot Python, Scatter plot with specific colors

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