Background
For plotting 2-d spatial distribution of some attribute, contourf
and pcolormesh
are both good choice as a visualization form.
Due to the actual meaning of the plotting data, I want to set the value nearby zero are showing in white color(for example, [-0.2,0.2]); and zero value should be the midpoint
From the question [Defining the midpoint of a colormap in matplotlib, I tried to set vmin
and vmax
to adjust the colormap.
Data import
import pygrib
grib='fnl_20141110_12_00.grib2';
grbs=pygrib.open(grib)
grb = grbs.select(name='Vertical velocity')[8]#Vertical velocity
data=grb.values
lat,lon = grb.latlons()
1. Contourf plot
m = Basemap(llcrnrlon=110,llcrnrlat=34,urcrnrlon=122,urcrnrlat=44,projection='mill')
fig=plt.figure(figsize=(8,4))
ax1 = plt.subplot(121)
x, y = m(lon, lat)
cs = m.contourf(x,y,data,cmap=plt.cm.RdBu)
cb = m.colorbar(cs,"bottom", size="5%", pad="8%")
ax1.set_title("Original",fontsize = 15)
ax2 = plt.subplot(122)
cs = m.contourf(x,y,data,cmap=plt.cm.RdBu,vmin = -1.8,vmax =1.8)
cb = m.colorbar(cs,"bottom", size="5%", pad="8%")
ax2.set_title("symmetrical vmin & vmax",fontsize = 15)
2. Pcolormesh plot
m = Basemap(llcrnrlon=110,llcrnrlat=34,urcrnrlon=122,urcrnrlat=44,projection='mill')
fig=plt.figure(figsize=(8,4))
ax1 = plt.subplot(121)
cs = m.pcolormesh(x,y,data,cmap=plt.cm.RdBu)
cb = m.colorbar(cs,"bottom", size="5%", pad="8%")
ax1.set_title("Original",fontsize = 15)
ax2 = plt.subplot(122)
cs = m.pcolormesh(x,y,data,cmap=plt.cm.RdBu,vmin = -1.8,vmax =1.8)
cb = m.colorbar(cs,"bottom", size="5%", pad="8%")
ax2.set_title("symmetrical vmin & vmax",fontsize = 15)
I upload the data here as an .grib2 file. If you are interested, you can download it
My doubt
With the contourf plot type, setting symmetrical
vmin
andvmax
couldn't adjust zeron to the midpoint of the colormap. That's different from pcolormesh.The same colormap
RdBu_r
==> different result incontourf
andpcolormesh
. Inpcolormesh
plot, the middle partof colorbar are shown inwhite color
. But incontourf
plot, the white color seemed to be ignored.