[PYTHON]在一個figure中畫兩組圖,以及兩個colorbar

畫出來效果是這樣的


好美對不對?

左邊和右邊的圖對應的colorbar是不同的。

如果每一個子圖也畫一個colorbar那會很浪費空間,所以在最底下的地方畫colorbar,因此要自己手動設colorbar的位置,主要是用這個參數cax=plt.axes([0.65, 0.06, 0.3, 0.02])

不多說了,上代碼了。

def manycs2():
  N1=7
  N2=2
  ww = 0.4
  hh = 0.12
  for i_hour in range(0,25,1):
    fig=plt.figure()
    for i1 in range(0,N1):
      for i2 in range(0,N2):
        print(i_hour,i1,i2)
        da=nc.Dataset("C:\\Users\\kong\\PycharmProjects\\southcnseapycode\\crosssection\\4draw_crosssection_%d"%(i1+1))
        u=da.variables["u"][:]
        v=da.variables["v"][:]
        z=da.variables["z"][:]
        w=da.variables["w"][:]
        llj=da.variables["llj"][:]
        lat=da.variables["lat"][:]
        lon=da.variables["lon"][:]
        lon001=da.variables["lon001"][:]
        lat001=da.variables["lat001"][:]
        theta=da.variables["theta"][:]
        da.close()
        uv1=u*np.cos(theta)+v*np.sin(theta)##x
        uv2=-u*np.sin(theta)+v*np.cos(theta)##y
        lonlon=np.zeros((np.shape(z)[1],np.shape(lon)[0]))
        for i in range(0,np.shape(z)[1]):
          lonlon[i,:]=lon

        #--u--#
        a = fig.add_axes([0.075+i2*ww, 0.10 + i1*hh, ww, hh])
        if i2==0:
          cslev = np.arange(-6,7,1)#13
          cs=plt.contourf(lonlon,z[i_hour,:,:],uv1[i_hour,:,:],cslev,cmap=cm.coolwarm,extend='both')
        else:
          cslev = np.arange(0,10,1)#13
          cs=plt.contourf(lonlon,z[i_hour,:,:],uv2[i_hour,:,:],cslev,cmap=cm.coolwarm,extend='both')
        cs.cmap.set_over('k')
        cs.cmap.set_under('b')
        #cbar = plt.colorbar(cs)
        #cbar.set_label('u\'(m/s)')
        plt.ylim([0,4000])
        if i2==0:
          plt.yticks([0,1000,2000,3000],size=10)
        else:
          plt.yticks([],size=10)
        plt.xticks(size=5)
        ##--vector--##
        vectorsize=5
        rho=3
        Q = plt.quiver(lonlon[:,::rho],z[i_hour,:,::rho],uv1[i_hour,:,::rho],100000*w[i_hour,:,::rho],width=vectorsize,headwidth=vectorsize*2/3,headlength=vectorsize/2,headaxislength=vectorsize/2,minlength=0,units='dots',angles='xy', scale_units='xy', scale=100,minshaft=0.5)#100
        if i1==6:
          if i2==0:
            cbar=fig.colorbar(cs,orientation='horizontal',cax=plt.axes([0.05, 0.06, 0.3, 0.02]))
            fig.text(0.35, 0.03,'u\'(m/s)',size=15)
          else:
            cbar=fig.colorbar(cs,orientation='horizontal',cax=plt.axes([0.65, 0.06, 0.3, 0.02]))
            fig.text(0.55, 0.03,'v\'(m/s)',size=15)
    if i_hour==24:
      fig.text(0.4, 0.95,"average",size=20)
    elif i_hour+7<24:
      fig.text(0.4, 0.95,"%d UTC, %d localtime"%(i_hour,i_hour+7),size=20)
    else:
      fig.text(0.4, 0.95,"%d UTC, %d localtime"%(i_hour,i_hour+7-24),size=20)
    fig.text(0.45, 0.03,"lon",size=30)
    plt.savefig('C:\\Users\\kong\\PycharmProjects\\southcnseapycode\\crosssection\\crosssection%d hour'%i_hour,dpi=500)
    plt.close()


你可能感兴趣的:(Python黑科技)