Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting range of colors in pcolormesh

In matplotlib's imshow(), the optional arguments vmax and vmin set the range of the colorbar.

Instead, in matplotlib.pyplot.pcolormesh, they seem to rescale the values plotted and leave the colorbar intact.

I want to create a series of plots using pcolormesh with a fixed colorbar range, that corresponds to say [0.0,0.5], i.e. the minimum color should be at 0.0 and the maximum at 0.5. Additionally, I want the two edges of the colormap to be at [0.0,0.5], respectively.

I'm using:

thetas=[-4.86946861 -4.55530935 -4.24115008 -3.92699082 -3.61283155 -3.29867229 -2.98451302 -2.67035376 -2.35619449 -2.04203522 -1.72787596 -1.41371669 -1.09955743 -0.78539816 -0.4712389  -0.15707963  0.15707963  0.4712389 0.78539816  1.09955743  1.41371669]
radii=[[ 0.],[10.]]
values=[[0.00049802 0.0008128  0.00177386 0.00426617 0.01010776 0.02224737 0.04396539 0.07626507 0.11427899 0.14629945 0.15897023 0.14629945 0.11427899 0.07626507 0.04396539 0.02224737 0.01010776 0.00426617 0.00177386 0.0008128 ]]
fig, ax = plt.subplots(1, 1, subplot_kw={'polar':True})
ax.pcolormesh(thetas, radii, values,vmin=0.0,vmax=0.5,cmap='cool')

But the darkest color of the image is not magenta but the middle value between cyan and magenta.

like image 770
python_freak Avatar asked Oct 26 '25 10:10

python_freak


1 Answers

I think I figured that out:

# plot data on a ring
vmin,vmax = 0.0,0.5
norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
mesh = ax.pcolormesh(thetas, radii, values,norm=norm, **kwargs)

# add colorbar
cb=fig.colorbar(mesh,norm=norm)
like image 112
python_freak Avatar answered Oct 29 '25 00:10

python_freak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!