用标记替换等高线中的水平标签

replace labels of levels in contour with a marker

在matplotlib中是否可以用圆圈和星号等标记替换等高线中的水平文本? 我正在使用此代码来显示级别。但是我想在情节中用星星代替零!

levels = [0,10]

cs1 = ax0.contourf(p, l,p_difference,levels, cmap=cmap, vmin=vmin, vmax=vmax)
cs = ax0.contourf(p, l, p_difference, cmap=cmap, vmin=vmin, vmax=vmax)

ax0.clabel(cs1, fmt='%2.1d', colors='k', fontsize=12)  # contour line labels

clabel()fmt参数可以是一个将级别映射到字符串的字典。这样的字符串可以使用所用字体中可用的任何 unicode 字符。这是一个例子:

from matplotlib import pyplot as plt
import numpy as np

p = np.linspace(0, 10, 400)
l = np.linspace(0, 10, 400)
pp, ll = np.meshgrid(p, l)

p_difference = np.sin(pp + 0.06 * np.random.randn(400, 1).cumsum(axis=0)) \
               * np.cos(ll + 0.06 * np.random.randn(1, 400).cumsum(axis=1)) * 6 + 5

levels = [0, 10]

vmin = -1
vmax = 11
cs1 = plt.contour(p, l, p_difference, levels=levels, cmap='seismic', vmin=vmin, vmax=vmax)
cs = plt.contourf(p, l, p_difference, levels=np.arange(-1, 12, 1), cmap='seismic', vmin=vmin, vmax=vmax)

fmt = {0: '☆', 10: '★'}
plt.clabel(cs1, fmt=fmt, colors='w', fontsize=20)
plt.colorbar(cs)
plt.show()