How to make black/white contour plot with contours labeled

I’m trying, using hvplot and xarray, to make a contour plot like this matplotlib example (see cell 8):

def f(x, y):
    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

contours = plt.contour(X, Y, Z, 3, colors='black')
plt.clabel(contours, inline=True, fontsize=8)


I’ve managed to get rid of the colorbar, but I’m not finding a way to set all the contours to black and label them with text. I’ve tried:

from matplotlib.colors import ListedColormap
ds_dummy = xr.Dataset({'elevation': (['x', 'y'],  Z)},
                coords={'lon': (['x', 'y'], X),
                         'lat': (['x', 'y'], Y)})
black = np.zeros((1, 4))
cmap_black = ListedColormap(black)
hv_contours = ds_dummy.hvplot.contour('lon', 'lat', 'elevation').opts(cmap=cmap_black,

This produces a blank plot. Replacing cmap_black with “RdBu” gets the contours back, in reds and blues.

Is there some way to extract the matplotlib.contour.QuadContourSet from hv_contours to pass to one of matplotlib’s labeling functions?

I had a contour labelling almost working at some point but I’d have to dig around to find the code. Here’s an issue about the feature. So for now I can only really suggest how to see the contours to black:

ds_dummy.hvplot.contour('lon', 'lat', 'elevation', cmap=['#000000'], colorbar=False)
1 Like

oh, ok, I missed that issue somehow. Thanks!

And thanks, as always, to the devs for all this work. holoviz is a cool tool.