简体   繁体   中英

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM