0

So, after searching for a few hours, I can't seem to find a simple answer to my issue. I have a filled contour plot (contourf) for whom I want the values displayed on the colorbar to be between -45 and 45.

fig, ax = plt.subplots()
plt.contourf(time_array, f, half_CP, 30, cmap=cm.seismic, vmin=-45, vmax=45)
cb = plt.colorbar()
plt.clim(-45,45)

clim has successfully compressed the range of colors displayed in my colorbar, however my colorbar is still displaying values from around -80 to 80... in other words, my vmin/vmax isn't working. I've tried putting this argument in the plt.colorbar line, however that has had no effect either.

I would not be surprised if it's something super-simple, but I've been trying to find the error/mistake/what I'm missing for a few hours now. I would appreciate any help!

Yoshi
  • 671
  • 8
  • 20
  • I think you can manually set the ticks for the colorbar, as in [this example](http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html). But that's probably not the easiest/best solution. –  Jan 18 '16 at 01:56
  • Doing that does set the proper tick locations, but doesn't shorten the colorbar, sadly. – Yoshi Jan 18 '16 at 03:15

1 Answers1

-1

I usually define a number of levels to contour, which also limits the range of the colorbar.

levels = np.arange(-45,45,1)
CS1 = plt.contourf(x,y,z,levels,cmap=cm.get_cmap('RdYlBu_r',len(levels)-1), extend='both')
plt.colorbar(CS1,orientation='vertical',shrink=0.5, format='%.2f')

Hope this helps. Cheers, Trond

Trond Kristiansen
  • 2,379
  • 23
  • 48
  • Defining the number of levels does force the colorbar to plot between those values. However, this also takes much longer to render (I generally only use about 30 levels, not 90!). Surely there's an easier way to simply tell the colorbar to only plot values between -45 and 45. It works simply for the x and y axes, I'm confused as to why it's so much more difficult for this. – Yoshi Jan 18 '16 at 03:18
  • To use fewer levels just reduce them levels=no.arange(-45,45,3) – Trond Kristiansen Jan 18 '16 at 03:43