Use Colormaps and Outlines in Matplotlib Scatter Plots

When I first started using Matplotlib in Python, I loved how easy it was to create scatter plots. But as my data projects became more complex, I realized that plain scatter plots often failed to highlight patterns effectively.

That’s when I discovered the power of colormaps and outlines in Matplotlib scatter plots. These two simple visual enhancements can make an ordinary chart look professional, insightful, and presentation-ready.

In this tutorial, I’ll show you exactly how I use colormaps and outlines in my Python scatter plots. I’ll also share two different methods for each approach so that you can choose the one that fits your workflow best.

Understand Colormaps in Python Matplotlib Scatter Plots

When you’re working with large datasets, using a colormap can help you visually differentiate values based on a continuous variable (for example, income, temperature, or sales).

In Python’s Matplotlib, colormaps automatically assign colors based on numeric values, which makes your scatter plots more informative and visually appealing.

Method 1 – Use the c Parameter with a Built-in Colormap

The easiest way to apply a colormap in Matplotlib is to use the c parameter in the plt.scatter() function. You can pass a list or array of numeric values and specify a colormap using the cmap argument.

Here’s how I typically do it in my Python projects:

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
np.random.seed(42)
x = np.random.rand(50) * 10
y = np.random.rand(50) * 10
values = np.random.rand(50) * 100  # Values for colormap

# Create scatter plot with colormap
plt.figure(figsize=(8, 6))
scatter = plt.scatter(x, y, c=values, cmap='viridis', s=100, edgecolors='black')

# Add colorbar
plt.colorbar(scatter, label='Value Scale')

# Add labels and title
plt.title('Scatter Plot with Colormap in Python (Method 1)')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

plt.show()

I executed the above example code and added the screenshot below.

Colormaps in Matplotlib Scatter Plots

In this method, I used the ‘viridis’ colormap, which is one of my favorites for readability and accessibility. The c parameter maps colors to numeric values, while the colorbar() function helps interpret those colors easily.

This approach is ideal when you want to represent continuous data like temperature, population density, or sales performance across different regions.

Method 2 – Use Normalization and Custom Colormaps

Sometimes, I need more control over how colors are distributed across my data. In that case, I use normalization along with a custom colormap from Matplotlib’s cm module.

Here’s an example:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.colors import Normalize

# Generate sample data
np.random.seed(10)
x = np.random.rand(60) * 100
y = np.random.rand(60) * 100
values = np.random.rand(60) * 500

# Normalize the data
norm = Normalize(vmin=min(values), vmax=max(values))
colors = cm.plasma(norm(values))

# Create scatter plot
plt.figure(figsize=(8, 6))
plt.scatter(x, y, color=colors, s=120, edgecolors='gray')

# Add colorbar
sm = plt.cm.ScalarMappable(cmap='plasma', norm=norm)
sm.set_array([])
plt.colorbar(sm, label='Normalized Value')

plt.title('Custom Colormap with Normalization in Python (Method 2)')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

plt.show()

I executed the above example code and added the screenshot below.

Matplotlib Colormaps Scatter Plots

In this method, I used the ‘plasma’ colormap and normalized the data manually. Normalization ensures that the color mapping remains consistent even when the data range changes.

This technique is especially useful when comparing multiple datasets or when you want to highlight specific ranges of values in your scatter plot.

Add Outlines to Scatter Plot Markers in Python Matplotlib

While colormaps help represent data values, outlines make your scatter plot markers stand out visually.

Adding outlines around markers can improve contrast, especially when points overlap or when your background color is dark. It’s a small detail that makes a big difference in readability.

Method 1 – Use the edgecolors Parameter

The simplest way to add outlines is by using the edgecolors parameter in plt.scatter(). You can specify the color and width of the outline directly.

Here’s a quick example:

import matplotlib.pyplot as plt
import numpy as np

# Generate data
np.random.seed(5)
x = np.random.rand(40) * 50
y = np.random.rand(40) * 50
colors = np.random.rand(40)

# Create scatter plot with outlines
plt.figure(figsize=(8, 6))
plt.scatter(x, y, c=colors, cmap='cool', s=180, edgecolors='black', linewidths=1.5)

plt.title('Scatter Plot with Outlines using Edgecolors (Python Method 1)')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.colorbar(label='Color Scale')

plt.show()

I executed the above example code and added the screenshot below.

Outlines in Matplotlib Scatter Plots

Here, I set edgecolors=’black’ to create a clear boundary around each marker. The linewidths parameter controls how thick the outlines appear.

This method works great for presentations or reports where you want your scatter points to “pop” visually.

Method 2 – Add Marker Outlines Using a Second Scatter Layer

If you want more flexibility, you can layer two scatter plots, one for the outline and one for the fill color. This gives you full control over the size and appearance of the outline.

Here’s how I do it:

import matplotlib.pyplot as plt
import numpy as np

# Generate data
np.random.seed(15)
x = np.random.rand(50) * 100
y = np.random.rand(50) * 100
values = np.random.rand(50) * 200

# Create scatter plot with two layers
plt.figure(figsize=(8, 6))

# Outline layer
plt.scatter(x, y, s=250, color='black', alpha=0.6)

# Main scatter layer
plt.scatter(x, y, s=180, c=values, cmap='cividis', edgecolors='white', linewidths=1)

plt.title('Double Layer Scatter Plot with Outlines (Python Method 2)')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.colorbar(label='Data Values')

plt.show()

I executed the above example code and added the screenshot below.

Matplotlib Outlines Scatter Plots

In this method, I first plotted a larger black scatter plot as the base layer (for the outline) and then overlaid a smaller scatter plot with the actual colors.

This double-layer technique gives a polished, professional look that’s perfect for dashboards, reports, or data presentations.

Combine Colormaps and Outlines in a Single Scatter Plot

Now that we’ve covered both concepts individually, let’s combine colormaps and outlines to create a visually stunning scatter plot in Python.

import matplotlib.pyplot as plt
import numpy as np

# Generate data
np.random.seed(25)
x = np.random.rand(100) * 10
y = np.random.rand(100) * 10
values = np.random.rand(100) * 50

# Create scatter plot with colormap and outlines
plt.figure(figsize=(9, 7))
scatter = plt.scatter(x, y, c=values, cmap='magma', s=150, edgecolors='white', linewidths=1.2)

# Add colorbar
plt.colorbar(scatter, label='Value Intensity')

# Add labels and title
plt.title('Python Scatter Plot with Colormap and Outlines Combined')
plt.xlabel('Longitude (USA Sample Data)')
plt.ylabel('Latitude (USA Sample Data)')

plt.show()

This example ties everything together: a ‘magma’ colormap to represent intensity and white outlines to make the points stand out beautifully.

I often use this approach when visualizing geographic or demographic data, especially when plotting multiple cities or regions across the USA.

Tips for Using Colormaps and Outlines Effectively

Here are a few best practices I’ve learned over the years while working with scatter plots in Python:

  • Choose accessible colormaps: Use colorblind-friendly options like viridis, cividis, or plasma.
  • Use outlines for visibility: Dark outlines work best on light backgrounds, and white outlines work best on dark backgrounds.
  • Normalize your data: Always normalize your values before applying colormaps to maintain consistent color mapping.
  • Add colorbars: A colorbar helps viewers interpret your data quickly.
  • Keep it simple: Avoid using too many colors or overly thick outlines; subtlety often looks more professional.

When you start using colormaps and outlines in your Matplotlib scatter plots, you’ll notice an immediate improvement in how your data is perceived.

These visual enhancements help your audience focus on insights rather than raw numbers, making your Python visualizations not only functional but also beautiful.

You may also like to read other Matplotlib tutorials:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.