abby-baskind / seniorthesis

0 stars 2 forks source link

subfigs for polar stereographic projections #13

Open abby-baskind opened 3 years ago

abby-baskind commented 3 years ago

as a disclaimer, this is not a huge issue. it's mostly an annoyance. but the way i'm doing my polar stereographic projections currently plots each projection as its own figure, such that when i go to save my figure, it only saves a single fig. For instance, fgco2_stereoproj_july19

however, i want all the plots to be subfigs on the same fig so i can save them as one figure. i was able to do this successfully with meridional slices of the pacific

so for context, i started with a function (nearly identical to something i stole from graeme)

def draw_SOcustom(X,Y,Z,cmap,cbar_label,title,plot_type,**kwargs):

    crs_plot = ccrs.SouthPolarStereo()
    crs_source = ccrs.PlateCarree()

    fig,ax = plt.subplots(figsize=(10,10),subplot_kw={'projection':crs_plot})
    ax.add_feature(cfeature.LAND,zorder=10,facecolor='darkgray')
    ax.set_extent([-180, 180, -90, -30], crs_source)
    # Compute a circle in axes coordinates, which we can use as a boundary
    # for the map. We can pan/zoom as much as we like - the boundary will be
    # permanently circular.
    theta = np.linspace(0, 2*np.pi, 100)
    center, radius = [0.5, 0.5], 0.5
    verts = np.vstack([np.sin(theta), np.cos(theta)]).T
    circle = mpath.Path(verts * radius + center)
    ax.set_boundary(circle, transform=ax.transAxes)

    ax.gridlines()
    ax.coastlines()

    ax.set_title(title,fontsize=14,pad = 20)

    if plot_type == 'contourf':
        im = ax.contourf(X,Y,Z,kwargs['clevs'],cmap=cmap,transform=crs_source)
    elif plot_type == 'pcolormesh':
        im = ax.pcolormesh(X,Y,Z,cmap=cmap,transform=crs_source,vmin=kwargs['clims'][0],vmax=kwargs['clims'][1])

    cbar = plt.colorbar(im,orientation='horizontal',fraction=0.025,pad=0.05)
    cbar.set_label(cbar_label,fontsize=12)

#     plt.show()

    return fig,ax

So i tried to base a solution on that and did this

fig, axarr = plt.subplots(nrows = 7, figsize=[18,12],subplot_kw={'projection':ccrs.SouthPolarStereo()})
ax_idx = 0 # index for your axes array
for name, ds_fgco2 in dd_fgco2.items():
    crs_plot = ccrs.SouthPolarStereo()
    crs_source = ccrs.PlateCarree()
    ax = axarr.flat[ax_idx]
    #fig,ax = plt.subplots(figsize=(10,10),subplot_kw={'projection':crs_plot})
    ax.add_feature(cfeature.LAND,zorder=10,facecolor='darkgray')
    ax.set_extent([-180, 180, -90, -30], crs_source)
    # Compute a circle in axes coordinates, which we can use as a boundary
    # for the map. We can pan/zoom as much as we like - the boundary will be
    # permanently circular.
    theta = np.linspace(0, 2*np.pi, 100)
    center, radius = [0.5, 0.5], 0.5
    verts = np.vstack([np.sin(theta), np.cos(theta)]).T
    circle = mpath.Path(verts * radius + center)
    ax.set_boundary(circle, transform=ax.transAxes)

    ax.gridlines()
    ax.coastlines()

    title=ds_fgco2.attrs['source_id']+ ' '+ds_fgco2.attrs['variant_label']
    ax.set_title(title,fontsize=14,pad = 20)

    Z = ds_fgco2.fgco2.isel(time = 0)
    lat = ds_fgco2.lat.transpose('y','x')
    lon = ds_fgco2.lon.transpose('y','x')
    X = lon.data
    Y = lat.values

    im = ax.pcolormesh(X,Y,Z,cmap='RdBu_r',transform=crs_source,vmin=-3e9,vmax=3e9)

    cbar_label = 'Surface Downward Flux of Total CO2 (kg m-2 s-1)'
    cbar = plt.colorbar(im,orientation='horizontal',fraction=0.025,pad=0.05)
    cbar.set_label(cbar_label,fontsize=12)

    ax_idx += 1

And I get this

Screen Shot 2021-07-20 at 5 15 39 PM

Obviously there are a couple problems with this. First all the colorbars are piled at the bottoms, but when i change cbar = plt.colorbar(im,orientation='horizontal',fraction=0.025,pad=0.05) to cbar = plt.colorbar(im,ax=ax,orientation='horizontal',fraction=0.025,pad=0.05) it looks bad

Screen Shot 2021-07-20 at 5 22 11 PM

Second, and probably more importantly, the plots aren't plotting anything. they're empty :'( i know i must be missing something but i'm not sure what that thing is