How do you specify colors on a scatter plot in python?

The normal way to plot plots with points in different colors in matplotlib is to pass a list of colors as a parameter.

E.g.:

import matplotlib.pyplot
matplotlib.pyplot.scatter([1,2,3],[4,5,6],color=['red','green','blue'])

How do you specify colors on a scatter plot in python?

When you have a list of lists and you want them colored per list. I think the most elegant way is that suggesyted by @DSM, just do a loop making multiple calls to scatter.

But if for some reason you wanted to do it with just one call, you can make a big list of colors, with a list comprehension and a bit of flooring division:

import matplotlib
import numpy as np

X = [1,2,3,4]
Ys = np.array([[4,8,12,16],
      [1,4,9,16],
      [17, 10, 13, 18],
      [9, 10, 18, 11],
      [4, 15, 17, 6],
      [7, 10, 8, 7],
      [9, 0, 10, 11],
      [14, 1, 15, 5],
      [8, 15, 9, 14],
       [20, 7, 1, 5]])
nCols = len(X)  
nRows = Ys.shape[0]

colors = matplotlib.cm.rainbow(np.linspace(0, 1, len(Ys)))

cs = [colors[i//len(X)] for i in range(len(Ys)*len(X))] #could be done with numpy's repmat
Xs=X*nRows #use list multiplication for repetition
matplotlib.pyplot.scatter(Xs,Ys.flatten(),color=cs)

How do you specify colors on a scatter plot in python?

cs = [array([ 0.5,  0. ,  1. ,  1. ]),
 array([ 0.5,  0. ,  1. ,  1. ]),
 array([ 0.5,  0. ,  1. ,  1. ]),
 array([ 0.5,  0. ,  1. ,  1. ]),
 array([ 0.28039216,  0.33815827,  0.98516223,  1.        ]),
 array([ 0.28039216,  0.33815827,  0.98516223,  1.        ]),
 array([ 0.28039216,  0.33815827,  0.98516223,  1.        ]),
 array([ 0.28039216,  0.33815827,  0.98516223,  1.        ]),
 ...
 array([  1.00000000e+00,   1.22464680e-16,   6.12323400e-17,
          1.00000000e+00]),
 array([  1.00000000e+00,   1.22464680e-16,   6.12323400e-17,
          1.00000000e+00]),
 array([  1.00000000e+00,   1.22464680e-16,   6.12323400e-17,
          1.00000000e+00]),
 array([  1.00000000e+00,   1.22464680e-16,   6.12323400e-17,
          1.00000000e+00])]

You are here: Home / Python / Python Tips / How To Specify Colors to Scatter Plots in Python

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.

How do you specify colors on a scatter plot in python?

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.

How do you specify colors on a scatter plot in python?

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.

How do you specify colors on a scatter plot in python?

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.

How do you specify colors on a scatter plot in python?

How do you color code a scatter plot?

To edit the colours, select the chart -> Format -> Select Series A from the drop down on top left. In the format pane, select the fill and border colours for the marker. Repeat these steps for Series B and Series C.

Can you create a color scatter plot using matplotlib?

Use matplotlib. pyplot. scatter() to make a colored scatter plot. Using lists of corresponding x and y coordinates and a list colors , create a list color_indices that assigns each coordinate the index of a color from colors .

What is the function to give color to plot in Python?

colors() This function is used to specify the color.

How do I change the marker color in matplotlib?

All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12).

How do you add a color bar to a scatter plot?

Linked.
Plotting a 2D scatter plot with color heatmap..
Python/Matplotlib - Colorbar configuration..
matplotlib - set correct colorbar as for plot and colorbar number labels to correspond to real data range..
Using matplotlib.pyplot to color the plot using a continuous variable..
Making a simple colorbar..