Search code examples
pythonmatplotlibcontourcolorbar

Matplotlib: fixed boundaries on a colorbar of a polar contour plot


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.


Solution

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