How to set the border color of the dots in matplotlib's scatterplots?

To set the border color of dots in matplotlib scatterplots, use the edgecolors parameter in the scatter() method. This parameter controls the color of the dot borders, while linewidth controls their thickness.

Basic Syntax

plt.scatter(x, y, edgecolors='color_name', linewidth=width)

Example with Red Border

Here's how to create a scatterplot with red borders around the dots ?

import numpy as np
import matplotlib.pyplot as plt

# Generate sample data
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)

# Create scatterplot with red borders
plt.scatter(x, y, s=500, c=colors, 
           edgecolors='red', linewidth=3)

plt.title('Scatterplot with Red Borders')
plt.show()

Multiple Border Colors

You can also use different border colors for each dot by passing an array to edgecolors ?

import numpy as np
import matplotlib.pyplot as plt

# Generate sample data
N = 8
x = np.random.rand(N)
y = np.random.rand(N)

# Different border colors for each dot
border_colors = ['red', 'blue', 'green', 'orange', 
                'purple', 'brown', 'pink', 'gray']

plt.scatter(x, y, s=400, c='lightblue', 
           edgecolors=border_colors, linewidth=2)

plt.title('Scatterplot with Multiple Border Colors')
plt.show()

Border Color Options

Parameter Description Example Values
edgecolors Border color(s) 'red', 'black', ['red', 'blue']
linewidth Border thickness 1, 2, 3 (numbers)
c Fill color(s) 'blue', array of colors

Common Border Colors

Here are some commonly used border color combinations ?

import numpy as np
import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]

fig, axes = plt.subplots(2, 2, figsize=(10, 8))

# Black borders
axes[0,0].scatter(x, y, s=300, c='yellow', edgecolors='black', linewidth=2)
axes[0,0].set_title('Black Borders')

# White borders
axes[0,1].scatter(x, y, s=300, c='darkblue', edgecolors='white', linewidth=2)
axes[0,1].set_title('White Borders')

# No borders
axes[1,0].scatter(x, y, s=300, c='red', edgecolors='none')
axes[1,0].set_title('No Borders')

# Transparent borders
axes[1,1].scatter(x, y, s=300, c='green', edgecolors='gray', linewidth=1, alpha=0.7)
axes[1,1].set_title('Gray Borders with Transparency')

plt.tight_layout()
plt.show()

Conclusion

Use the edgecolors parameter to set border colors in matplotlib scatterplots. Combine it with linewidth to control border thickness. You can use single colors, color arrays, or 'none' to remove borders entirely.

Updated on: 2026-03-26T02:58:52+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements