How to Create a Gradient Color Fill Between Areas in Matplotlib

What will you learn?

  • Learn to create a smooth gradient color transition between different areas of a plot using Matplotlib.
  • Enhance the visual appeal of plots by filling regions with gradient colors.

Introduction to the Problem and Solution

In this tutorial, delve into the art of filling areas with gradient colors in Matplotlib. Elevate your data visualization game by emphasizing transitions and highlighting specific regions with gradients. By harnessing Matplotlib’s customization features, you can craft visually stunning and informative plots.


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

# Generate sample data points
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Create a figure and axis
fig, ax = plt.subplots()

# Custom gradient color shading between y1 and y2
gradient_colors = ['blue', 'cyan', 'green']
cmap_name = 'custom_gradient'
n_bins = 100    
cmap = LinearSegmentedColormap.from_list(cmap_name, gradient_colors)
ax.fill_between(x, y1, y2,
                norm=plt.Normalize(0, 10))

# Copyright PHD


To achieve a seamless color transition between filled areas on a Matplotlib plot: 1. Generate data points defining region boundaries. 2. Create a custom colormap using LinearSegmentedColormap. 3. Utilize fill_between() method on the axes object for smooth color interpolation.

    How can I customize the range of my gradient?

    Adjust parameters within plt.Normalize() for different color mapping ranges.

    Can I use different color schemes?

    Yes! Specify any colors within your custom colormap for personalized gradients.

    Is adjusting opacity possible?

    Modify alpha values for transparency control within colors or colormaps.

    Will this work with other plot types?

    Adaptable to various plots like scatter or bar graphs where area filling is needed.

    How to add annotations or labels?

    Include text annotations or legends using functions like text() or legend() from Matplotlib.


    Mastering gradient-filled shades adds an artistic touch and clarity to plotted data in Python using Matplotlib. Experiment with diverse color combinations for tailored insights that enhance visualization quality while engaging viewers effectively.

    Leave a Comment