pyplot colormap with extend option (in contourf)
<p>I have a set of data ranging from -0.5 to 2 (for example). I only want to color the data that are within [-0.5,1]. I did something like:</p>

<pre><code>from matplotlib import colors as mpl_colors

color_map  =  plt.cm.jet
color_map.set_bad('gray',  1.0)
norm_color = mpl_colors.Normalize(vmin=-0.5, vmax=1, clip=False)
nb_colors = 40
map_frame.contourf(x_mesh, y_mesh, data, nb_colors, cmap=color_map, norm=norm_color, extend='both')
</code></pre>

<p>I expect the colorbars to have 40 colored strides, ranging from -0.5 to 1.</p>

<p>Instead I get the following image:</p>

<p><a href="https://i.stack.imgur.com/b4rU4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b4rU4.png" alt="Results with weird colorbar"></a></p>

<p>The colorbar doesnot stop at 1 as expected, like it does for example here: <a href="http://matplotlib.org/examples/pylab_examples/contourf_demo.html" rel="nofollow noreferrer">http://matplotlib.org/examples/pylab_examples/contourf_demo.html</a></p>

<p>Do you know why ?</p>