4

As it says in the title, I am trying to fix the values of the colorbar (vmin=-3 and vmax=+3) of a polar contour plot. I am going to generate several dozens of such graphs, and the auto scaling of the colorbar makes comparison very difficult.

The plot itself is generated by the following code:

fig, ax = subplots(subplot_kw=dict(projection='polar'))
cax = ax.contourf(thetas, r, values, 130)
cb1 = fig.colorbar(cax)

I have been going through http://matplotlib.sourceforge.org for hours and still haven't found the solution. I would point me in the right direction.

Sasha
  • 1,338
  • 2
  • 13
  • 22

2 Answers2

2

You can do this by passing in the contour levels yourself.

Instead of just trying to set vmin=3, vmax=3, pick 130 values between vmin and vmax so they will be the same for all the graphs, independent of the data range.

Try:

contour_levels = arange(-3, 3, 0.05)

fig, ax = subplots(subplot_kw=dict(projection='polar'))
cax = ax.contourf(thetas, r, values, contour_levels)
cb1 = fig.colorbar(cax)
Eric
  • 3,142
  • 17
  • 14
  • First of all, thank You for Your effort. Unfortunately adding that line of code does nothing. sometimes my data is going to be more then 3 or less then -3. I am having trouble understanding the motivation behind your Idea. My data set is 800 points. For each point I have (theta, r, value). The values of the 800 points sometimes exceed the range [-3,3] and sometimes they fit inside [-0.1,0.1]. – Sasha Aug 19 '12 at 16:58
  • I added a lines and changed the middle line. You need to both set contour_levels, and pass it into contourf instead of 130. By explicitly passing in the contour levels, you will make sure that the colorbar always covers those levels, instead of just going from the min to max of the data. – Eric Aug 19 '12 at 17:57
  • I missed that change. That did it. Thank alot for the help! – Sasha Aug 19 '12 at 18:05
1

An alternative solution might be to follow the logic used in this response to a similar question on setting the min and max of a colorbar. The main takeaway is the use of set_clim(self, vmin=None, vmax=None). In the context of this question, one of the following might work:

fig, ax = subplots(subplot_kw=dict(projection='polar'))
cax = ax.contourf(thetas, r, values, vmin=-3, vmax=3)
cb1 = fig.colorbar(cax)

OR

cb1.set_clim(vmin=-3, vmax=3)

This answer is in the same vein but addresses the requisite of using the same colorbar min/max for multiple graphs.

BLT
  • 415
  • 3
  • 9