如何使用 edgecolors 制作 matplotlib 等高线?

How to make matplotlib contour lines with edgecolors?

我想为 matplolib.pyplot.contour 中的线条添加边缘颜色。尝试了 edgecolors 和 markeredgecolors,没有效果。有人知道解决方案吗?

对于这种情况,您需要绘制数据集两次,第一次使用较粗的线宽(或较大的标记,具体取决于绘图类型你想到的)和 "outer" lines/markers 的颜色。然后,您再次绘制数据集,但使用更小的 lines/markers 和不同的颜色,即内线的颜色。

这里有一个例子,你可以复制粘贴来学习。该示例借鉴了 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)

这是许多体面的图表中常用的技术,用于突出显示数据(即使这个示例看起来很糟糕)。