Matplotlib scatter plot color

In this Python Matplotlib tutorial, we will discuss Matplotlib scatter plot color in matplotlib. Here we will cover different examples related to scatter plot color using matplotlib. And we will also cover the following topics:

  • Matplotlib scatter plot color
  • Python scatter plot color array
  • Python scatter plot color red
  • Matplotlib scatter plot edge color
  • Matplotlib scatter plot color each point
  • Matplotlib scatter plot color map
  • Matplotlib scatter plot marker face color
  • Matplotlib scatter plot color transparency
  • Python scatter plot random colors
  • Matplotlib scatter plot color by label
  • Python scatter plot color range
  • Matplotlib scatter plot two colors
  • Matplotlib scatter plot color label
  • Matplotlib scatter plot color rgb
  • Matplotlib scatter plot color by string
  • Matplotlib scatter plot color by category
  • Matplotlib scatter plot color by value
  • Matplotlib scatter plot color by category legend

Matplotlib scatter plot color

For data visualization, matplotlib provides a pyplot module, under this module we have a scatter() function to plot a scatter graph. And here we’ll learn how to color scatter plot depending upon different conditions.

The following steps are used to set the color to scatter plot:

  • Define Libraries: Import the important libraries which are required for the creation of the scatter plot. For visualization: pyplot from matplotlib and For data creation: NumPy.
  • Define Coordinates: Define x-axis and y-axis data coordinates, which are used for data plotting.
  • Plot a scatter graph: By using the scatter() function we can plot a scatter graph.
  • Set the color: Use the following parameters with the scatter() function to set the color of the scatter c, color, edgecolor, markercolor, cmap, and alpha.
  • Display: Use the show() function to visualize the graph on the user’s screen.

Also, check: Matplotlib 3D scatter

Python scatter plot color array

If we call a scatter() function multiple times, to draw a scatter plot, we’ll get each scatters of different colors. Here we’ll learn to set the color of the array manually, bypassing color as an argument.

The following is the syntax:

matplotlib.pyplot.scatter(x, y, color=None)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([5,7,8, 2,17])
y1 = np.array([99,103,87,94,78])
y2 = np.array([26, 23, 18, 55, 16])

# Scatter Plot color array

plt.scatter(x, y1, color='green')
plt.scatter(x, y2, color='red')

# Display

plt.show()

Explanation:

  • First, import the libraries such as matplotlib.pyplot and numpy for data visualization and data creation.
  • Next, we define the x, y1, and y2 data coordinates using array() function of numpy.
  • Then we use the scatter() function multiple times, to create a scatter plot.
  • We pass the color argument to the function to set the color manually.
  • To visualize the graph, use show() method.

Output:

python scatter plot color array
plt.scatter(color=None)

Read: Stacked Bar Chart Matplotlib

Python scatter plot color red

Here we’ll learn to draw a scatter plot with a single color format. We use the parameter c to set the color of the plot and here we’ll set it to red.

The following is the syntax:

matplotlib.pyplot(x, y, c='red')

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([2, 4, 6, 8, 10, 15, 12])

# Scatter Plot color red

plt.scatter(x, y, c='red')

# Display

plt.show()
  • Here we define x-axis and y-axis data coordinates using array() method of numpy.
  • Next, to get a single color scatter string format, we pass c argument to the scatter() method and set its value to red.
Python scatter plot color red
plt.scatter(c=’red’)

By default, we the scatter markers of blue color.

Read: Matplotlib two y axes

Matplotlib scatter plot edge color

We’ll see examples of scatter plots where we set the edge color of the plot. To set an edge color of the scatter markers, use the edgecolor parameter with the scatter() method.

The following is the syntax:

matplotlib.pyplot.scatter(x, y, edgecolor=None)

Example #1

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([1, 4, 5, 6, 7])
y = np.array([2, 4, 6, 15, 12])

# Edgecolor

plt.scatter(x, y, c='lime', edgecolor='black', s=500)

# Display

plt.show()
matplotlib scatter plot edge color
plt.scatter(edgecolor=’k’)

Here we pass the parameter edgecolor, color to the scatter() function and set its value to black and lime respectively.

Example #2

Here we’ll see an example, where we set the different edgecolors for each scatter marker.

The following are the steps:

  • Import library matplotlib.pyplot.
  • Next, define x and y axes data points.
  • Then create list of colors.
  • To plot a scatter graph, use scatter() function.
  • To set the different edgecolor for each scatter pass edgecolor parameter and set its value to given list of colors.

Output:

matplotlib scatter plot marker edge color
Different edgecolors for each scatter marker

Read: Horizontal line matplotlib

Matplotlib scatter plot color each point

We’ll see an example, where we set a different color for each scatter point. To set a different color for each point we pass a list of colors to the color parameter of the scatter() method.

Let’s see an example:

# Import Library


import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([1, 3, 5, 7, 9, 11])

# Color

color = ['lightcoral', 'darkorange', 'olive', 'teal', 'violet', 
         'skyblue']

# Set different color

plt.scatter(x, y, c=color, s=400)

# Display

plt.show()

The following are the steps:

  • Import library matplotlib.pyplot and numpy for data visualization and creation.
  • Next, define data axes using array() method of numpy.
  • Then create list of colors.
  • To plot a scatter graph, use scatter() function.
  • To set the different color for each scatter marker pass color parameter and set its value to given list of colors.
matplotlib scatter plot color each point
Different colors for each scatter marker

Read: Draw vertical line matplotlib

Matplotlib scatter plot color map

We’ll learn to create a scatter plot of x and y data coordinates with a color map. To add a color map call matplotlib.pyplot.scatter() method with cmap parameter.

The following is the syntax:

matplotlib.pyplot.scatter(x, y, c=None, cmap=None)

Let’s have a look at an example:

# Import Library


import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.linspace(0, 10, 100)
y = np.sin(x)

# Set color map

plt.scatter(x, y, c=y, cmap='Set2')

# Display

plt.show()

Explanation:

  • To define x-axis and y-axis data coordinates, we use linespace() and sin() function.
  • To create a scatter plot, we use scatter() method.
  • We pass c parameter to set the variable represented by color and cmap parameter to set the colormap.
matplotlib scatter plot color map
plt.scatter(cmap=’Set2′)

Read: Matplotlib invert y axis

Matplotlib scatter plot marker face color

We’ll learn to set the face color of the scatter markers. For this pass facecolors argument to scatter() method.

The following is the syntax:

matplotlib.pyplot.scatter(x, y, facecolors=None, edgecolors=None)

Example:

# Import Library


import matplotlib.pyplot as plt
import numpy as np

# Define Data


x = np.random.randn(100) 
y = np.random.randn(100)

# facecolor


plt.scatter(x, y, s=400, facecolors='none', edgecolors='green')

# Display

plt.show()

Here we set facecolors parameter to none so, we get the white color to scatter markers.

matplotlib scatter plot marker face color
plt.scatter(facecolors=’none’)

Read: Matplotlib save as pdf

Matplotlib scatter plot color transparency

Sometimes due to the overlapping of scatter markers, we are not able to check the density of the data plotted, so at that time we need transparency. The alpha argument is used to make the scatter markers transparent.

The following is the syntax:

matplotlib.pyplot.scatter(x, y, alpha=None)

Here alpha parameter set the transparency value. By default it values is 1 i.e. no transparency.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([99,86,88,111,103,87,94,78,77,85,86])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800])

# Transparency


plt.scatter(x, y, s=4500, facecolor='maroon', alpha=0.2)

# Display


plt.show()
  • Here we define data coordinates using array() function of numpy.
  • To create a scatter plot, use scatter() function and we also facecolors, s, and alpha as a parameter to set color of marker, size of marker, and transparency of marker respectively.
matplotlib scatter plot color transparency
plt.scatter(alpha=0.2)

Read: Matplotlib title font size

Python scatter plot random colors

Here we’ll see an example, where we create a scatter plot with random colors for each scatter marker point.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define data

x = np.random.randn(200) 
y = np.random.randn(200)

# Random colors for each point

plt.scatter(x, y, c=np.random.rand(len(x),3))

# Display

plt.show()

Here we use the random.rand() method of numpy and len() method to randomly color each scatter marker.

python scatter plot random colors
plt.scatter(c=np.random.rand(len(x),3)

Read: Matplotlib default figure size

Matplotlib scatter plot color by label

Here we’ll see an example of scatter plot markers color bt labels.

Example:

# Import Library

import matplotlib.pyplot as plt
from numpy.random import random

# Define colors

colors = ['maroon', 'teal', 'yellow']

# Plot

data1 = plt.scatter(random(30), random(30), marker='d', 
                    color=colors[0],label='Label 1')
data2 = plt.scatter(random(50), random(50), marker='d', 
                    color=colors[1],label='Label 2')
data3 = plt.scatter(random(25), random(25), marker='d', 
                    color=colors[1],label='Label 3')
data4 = plt.scatter(random(20), random(20), marker='d', 
                    color=colors[2],label='Label 4')
data5 = plt.scatter(random(10), random(10), marker='d', 
                    color=colors[0],label='Label 5')

# Add legend

plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.08), 
           ncol=2)

# Display

plt.show()
  • In the above example, first, we import matplotlib.pyplot and random library.
  • Next, we create a list of colors.
  • Then we use the scatter() method to create a scatter plot, and we also pass marker, color, and label as a parameter.
  • We define the data coordinates by using the random() function.
  • To add a legend to the plot, use the legend() method.
  • To set a position of legend outside the plot, we use the bbox_to_anchor() method.
matplotlib scatter plot color by label, Matplotlib scatter color by value
Color By Label

Read: Matplotlib Plot NumPy Array

Python scatter plot color range

By adding a colorbar to a scatter plot, we provide a range for numbers to colors based on the data plotted in the graph. To add a colorbar to a plot, call the colorbar() function.

Example:

# Import Library


import matplotlib.pyplot as plt
import numpy as np

# Define Data


x = np.linspace(0, 20, 200)
y = np.cos(x)

# Set different color


plt.scatter(x, y, c=x, cmap='tab20')

# Add Colorbar

plt.colorbar()

# Display

plt.show()
python scatter plot color range
plt.colorbar()

Read: Matplotlib set_xticks

Matplotlibb scatter plot two colors

Here we’ll see an example where we create a multiple scatter plot by using the scatter() method. To differentiate the plots, we use two colors of your choice.

Example:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [1, 2, 3, 4, 5, 6]
y = [4, 1, 9, 6, 9, 3]

# Plot 1 scatter plot

plt.scatter(x, y, c='indigo')

# Define Data


x = [5, 9, 7, 8, 10, 11]
y = [1, 3, 5, 2, 6, 9]

# Plot 2 scatter plot

plt.scatter(x, y, c='chocolate')

# Title

plt.title('Two color scatter Plot')

# Labels

plt.xlabel('X')
plt.ylabel('Y')

# Display


plt.show()

Here we use the scatter() function two times to create a scatter graph. We define different data coordinates for each scatter plot.

matplotlib scatter plot two colors
Scatter Plot Two Different Colors

Read: Matplotlib set_xticklabels

Matplotlib scatter plot color label

Here we are going to see an example where we set the color of the legend plotted with a scatter plot. To set the color, use facecolor argument with the legend() method.

The following is the syntax:

matplotlib.pyplot.legend(['labels'], facecolor=None)

Example:

# Import library


import matplotlib.pyplot as plt
import numpy as np
 
# Define Data

x = np.linspace(0,20,100)
y1 = np.cos(x)
y2 = np.exp(y1)

# Scatter Plot

plt.scatter(x, y1)
plt.scatter(x, y2)

# Add legend and change color

plt.legend(["Cos X" , "Exponent of Cos X"], facecolor='bisque', 
           loc='upper center', bbox_to_anchor=(0.5, -0.08), 
           ncol=2)

# Display

plt.show()
  • Here we define data coordinates using linespace(), cos(), and exp() method of numpy.
  • To create a scatter plot, we use the scatter() method.
  • To add a legend to a plot, we use the legend() method.
  • To set the color of the legend, we pass facecolor parameter to the method.
matplotlib scatter plot color label
plt.legend(facecolor=None)

Read: Matplotlib fill_between

Matplotlib scatter plot color rgb

Here we plot a scatter plot by using the scatter() method and we also set the different colors for each marker so we pass a color parameter to the method.

In this example, we create a list of colors by using RGB color hex code.

Example:

# Import Library


import numpy as np
import matplotlib.pyplot as plt

# Define Data

x = np.array([99,86,88,111,103,87])
y = np.array([20,50,200,500,1000,60])

# Color RGB hex code


color = ['#FF8C00', '#FFD700', '#DAA520','#00FF7F' ,'#20B2AA', 
         '#8B008B']

# Plot

plt.scatter(x, y, c=color)

# Display

plt.show()
matplotlib scatter plot color rgb
RGB Hex Code

Read: Matplotlib set_yticklabels

Matplotlib scatter plot color by string

Here’s an example of how to make a scatter graph and color the scatter markers on the string’s basis.

Let’s see an example:

# Import Library

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

# Define Data

females = np.random.rand(150)
males = np.random.randint(100,600,150)
country =['Australia','Argentina', 'Canada', 'Egypt', 
          'Greece']*30

# Create DataFrame

df = pd.DataFrame(dict(females=females, males=males, country = 
                       country))

# Define colors 

colors = {'Australia':'r','Argentina':'y', 'Canada':'c', 
          'Egypt':'g', 'Greece':'m'}

# Scatter Plot

plt.scatter(df['females'], df['males'], 
            c=df['country'].map(colors))

# Display

plt.show()
  • In the above example, firstly we import the important libraries.
  • Next, we define data coordinates using random.rand() and random.randint() functions.
  • To create a data frame, we use the DataFrame() function of pandas.
  • Then we create a list of colors on the basis of string.
  • To plot a scatter graph, we use the scatter() function.
matplotlib scatter plot color by string
plt.scatter()

Read: Matplotlib tight_layout

Matplotlib scatter plot color by category

In this section, we are going to learn how to color the scatter markers by category.

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy
 
# Define Data Points

data = numpy.array([[2, 4, 6, 12, 5, 16, 11, 3, 9, 1, 7, 10],
                 [4, 2, 3, 7, 9, 1, 6, 5, 8, 15, 18, 13],
                 [3.5, 4, 12, 19, 20, 14, 2.9, 5.5, 9, 4, 14.2, 
                  5]])
 
# Category

categories = numpy.array([0, 1, 2, 1, 3, 2, 1, 0, 1, 3, 2, 0])
 
# Colormap


colormap = numpy.array(['r', 'y', 'm', 'c'])
 
# Plot

plt.scatter(data[0], data[1], s=100, c=colormap[categories])
plt.scatter(data[0], data[2], s=100, c=colormap[categories])

# Display


plt.show()
  • In the above example, we use the array() method of numpy to define data coordinates.
  • After this, we define categories by using array() method.
  • Then we define list of colors by using array() method.
  • To plot a scatter graph, we use the scatter() function.
matplotlib scatter plot color by category
Color By Category

Read: Matplotlib x-axis label

Matplotlib scatter plot color by value

In this section, we’ll see an example where we want to color the scatter markers based on some third variable or value.

Example:

# Import Library


import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([5, 10, 15, 20, 25, 30])
y = np.array([99, 100, 78, 55, 65, 62])
z = np.array([1, 2, 1, 2, 2, 1])

# Scatter Plot color by value


plt.scatter(x, y, s=350, c=z, cmap='jet')

# Display

plt.show()
matplotlib scatter plot color by value
Color By Third Variable

Read: Python Matplotlib tick_params

Matplotlib scatter plot color by category legend

In this section, we’ll see an example where we want to color the scatter markers based on category and we also add legend according to category.

Let’s see an example:

# Import Libraries

import pandas as pd
import matplotlib.pyplot as plt


# Create Data Frame

df = pd.DataFrame({'girls': [10, 12, 14, 16, 9, 8, 5, 6],
                   'boys': [2, 9, 11, 2.6, 5, 4, 10, 8],
                   'grades': ['A', 'A', 'A', 'B', 'B', 'C', 
                              'C', 'C']})

# group by category


groups = df.groupby('grades')
for grade, group in groups:
    plt.scatter(group.girls, group.boys, label=grade)
    
# Add legend

plt.legend()

# Display the graph

plt.show()
  • In the above example, we import matplotlib.pyplot and pandas libraries.
  • Next, we define the data coordinates using DataFrame() method of pandas.
  • To draw a scatter plot, we use the scatter() method.
  • groupby clause is used for dividing into groups.
  • To add a legend to a plot, we use the legend() function.
matplotlib scatter plot color by category legend
Category With Legend

You may also like to read the following tutorials on Matplotlib.

So, in this Python tutorial, we have discussed the “Matplotlib Scatter Plot Color” and we have also covered some examples related to it. These are the following topics that we have discussed in this tutorial.

  • Matplotlib scatter plot color
  • Python scatter plot color array
  • Python scatter plot color red
  • Matplotlib scatter plot edge color
  • Matplotlib scatter plot color each point
  • Matplotlib scatter plot color map
  • Matplotlib scatter plot marker face color
  • Matplotlib scatter plot color transparency
  • Python scatter plot random colors
  • Matplotlib scatter plot color by label
  • Python scatter plot color range
  • Matplotlib scatter plot two colors
  • Matplotlib scatter plot color label
  • Matplotlib scatter plot color rgb
  • Matplotlib scatter plot color by string
  • Matplotlib scatter plot color by category
  • Matplotlib scatter plot color by value
  • Matplotlib scatter plot color by category legend