Reputation: 2038
I want to generate a filled contour plot in Matplotlib (v3.1.0
) and highlight only a narrow range of values by limiting the colour range. In my current approach, the colourbar extends to the full range of values, and I want to limit this range to (vmin, vmax)
and use extend
to indicate that the colours are clipped. See the following example:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 1, 100)
y = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2
fig, ax = plt.subplots(nrows=1, ncols=1)
CS = ax.contourf(x, y, Z.T, levels=200, vmin=0.9, vmax=1.1, cmap="RdBu")
cb0 = fig.colorbar(CS, ax=ax, ticks=[0.9, 1.0, 1.1], extend="both")
plt.tight_layout()
plt.show()
As you can see, the colourbar is not limited to (0.9, 1.1)
, and does not extend
either (the arrows do not appear). Any ideas?
Upvotes: 1
Views: 2016
Reputation: 339765
You need to set the levels to the range of values you're interested in.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 1, 100)
y = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2
fig, ax = plt.subplots(nrows=1, ncols=1)
CS = ax.contourf(x, y, Z.T, levels=np.linspace(0.9,1.1,51),
vmin=0.9, vmax=1.1, cmap="RdBu", extend='both')
cb0 = fig.colorbar(CS, ax=ax, ticks=[0.9, 1.0, 1.1], extend="both")
plt.tight_layout()
plt.show()
Upvotes: 1