AKG
AKG

Reputation: 2479

How to make matplotlib contour lines with edgecolors?

I would like to add edgecolors to the lines in matplolib.pyplot.contour. Tried edgecolors and markeredgecolors, without effect. Does anyone know a solution?

Upvotes: 0

Views: 2236

Answers (1)

Oliver W.
Oliver W.

Reputation: 13459

For a case such as this, you'll want to plot the dataset twice, the first time with a thicker linewidth (or larger markers, depending on what type of plot you had in mind) and in the color of the "outer" lines/markers. Then, you plot the dataset again, but with smaller lines/markers and in a different color, the color of the inner line.

Here's an example that you can copy-paste to study. The example borrows from the matplotlib contour demo:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

# generate some sample data
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

plt.figure()
# plot the outer lines thicker
whites = plt.contour(X, Y, Z, colors='white', linewidths=7)
plt.gca().set_axis_bgcolor('red')  # you spoke of a red bgcolor in the axis (yuck!)
# and plot the inner lines thinner
CS = plt.contour(X, Y, Z, colors='red', linewidths=3)

This is a commonly used technique in many decent graphs, to highlight the data (even though this example looks awful).

Upvotes: 2

Related Questions