faraa
faraa

Reputation: 575

replace labels of levels in contour with a marker

Is it possible to replace texts of levels in a contour with markers like circle and star in matplotlib? I am using this code for showing levels. But I want to replace zero with a star in the plot!

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

Upvotes: 0

Views: 159

Answers (1)

JohanC
JohanC

Reputation: 80339

The fmt parameter of clabel() can be a dictionary mapping a level to a string. Such string can use any unicode character available in the font used. Here is an example:

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()

example plot

Upvotes: 1

Related Questions