python - Overlay pcolormeshes in matplotlib -
i want overlay differently colored regions pcolormesh. masked regions indeed not show up, cover other complementary regions. below example first plot both regions separately (so masking works nicely), want overlay them, second 1 covers first one. how can plot different regions colored differently?
#!/usr/bin/env python import numpy np import matplotlib.pyplot plt xi=np.linspace(0,10,100) yi=np.linspace(0,10,150) x=0.5*(xi[1:]+xi[:-1]) y=0.5*(yi[1:]+yi[:-1]) x,y=np.meshgrid(x,y) z = np.exp(-(x-5)**2-(y-5)**2) z1 = z.copy() z1[(x+y)<10]=np.nan z2 = z.copy() z2[(x+y)>=10]=np.nan plt.figure(figsize=(4,12),tight_layout=true) plt.subplot(3,1,1) plt.pcolormesh(x,y,z1,cmap='greens',vmin=0,vmax=z.max()) plt.subplot(3,1,2) plt.pcolormesh(x,y,z2,cmap='blues',vmin=0,vmax=z.max()) plt.subplot(3,1,3) plt.pcolormesh(x,y,z1,cmap='greens',vmin=0,vmax=z.max()) plt.pcolormesh(x,y,z2,cmap='blues',vmin=0,vmax=z.max())
edit:
i should add whole thing works nice if use contourf
instead of pcolormesh
, there ugly empty area in between them, shown below. alternate question: how rid of area when using contourf
?
you need make masked array numpy masking module instead of doing np.nan. change z2[(x+y)>=10]=np.nan
z2 = np.ma.masked_array(z, (x+y)>=10)
.
Comments
Post a Comment