How to plot the outline of the outer edges on a Matplotlib line in Python?

To plot the outline of the outer edges on a Matplotlib line in Python, we can create a visual effect by layering two lines with different widths and colors. This technique creates an outlined appearance by drawing a thicker background line followed by a thinner foreground line.

Basic Outline Effect

The key is to plot the same line twice − first with a thicker line width for the outline, then with a thinner line width for the main line ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create data points
x = np.linspace(-10, 10, 100)
y = np.sin(x)

# Plot outline (thicker line) first
plt.plot(x, y, linewidth=10, color='red', label='Outline')
# Plot main line (thinner line) on top
plt.plot(x, y, linewidth=5, color='yellow', label='Main line')

plt.title('Line with Outline Effect')
plt.legend()
plt.show()

Custom Outline Colors

You can experiment with different color combinations to create various visual effects ?

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 4*np.pi, 200)
y = np.cos(x) * np.exp(-x/8)

# Create subplot for multiple examples
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))

# Example 1: Black outline with blue line
ax1.plot(x, y, linewidth=8, color='black')
ax1.plot(x, y, linewidth=4, color='blue')
ax1.set_title('Black Outline with Blue Line')
ax1.grid(True, alpha=0.3)

# Example 2: White outline with green line
ax2.plot(x, y, linewidth=8, color='white')
ax2.plot(x, y, linewidth=4, color='green')
ax2.set_title('White Outline with Green Line')
ax2.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

How It Works

The outline effect works through layering:

  • Step 1: Draw the background line with a larger linewidth
  • Step 2: Draw the foreground line with a smaller linewidth on top
  • Result: The background line creates a visible border around the foreground line

Key Parameters

Parameter Purpose Typical Values
linewidth Controls line thickness Outline: 8-12, Main: 3-6
color Sets line color Contrasting colors work best
Plot order Outline first, main line second Thicker ? Thinner

Conclusion

Creating outlined lines in Matplotlib is achieved by plotting the same data twice with different line widths and colors. Plot the thicker outline first, then the thinner main line on top for the best visual effect.

Updated on: 2026-03-26T00:02:52+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements