Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

python - re-order the sequence of rows plotted in a figure with subplots using xarray dataset

I am trying to re-order the sequence of rows in my plot using xarray. The variable assigned to the row is scen_new and has 4 levels (0,1,2,3). The code is plotting it in ascending order. I want to plot one specific order or rows: 0,2,3,1. Do you know how to do that? I tried also using 'row_order' but did not work.

Here is the structure of my xarray dataset (data2)

#Plotting mean biomass
a = df.groupby(['irr', 'lat', 'lon', 'cultivar'],as_index=False).mean()
map_proj = ccrs.Mercator(central_longitude=-95)

p = data2.biomass.mean(dim=('time'), skipna=True).plot(x='lon', y='lat', col='irr', row='scen_new',
    transform=ccrs.PlateCarree(central_longitude=0),
    subplot_kws={'projection': map_proj}, vmin=a.biomass.quantile(0.001),
    vmax=a.biomass.quantile(0.999),
    figsize=(10,16), #rasterized=True
    # The remaining kwargs customize the plot just as for not-faceted plots
    robust=True,
    cmap=mpl.cm.RdYlGn,
    cbar_kwargs={
        "orientation": "vertical",
        "shrink": 0.7,
        "aspect": 25,
        "pad": 0.05, #distance of colorbar to plot
        "label": "Biomass ($kg,ha^{-1}$)"}, zorder=1, add_colorbar=False, label=False)

def area(ax, iso, clr) :
    shp = shpreader.natural_earth(resolution='10m',category='cultural',
                                  name='admin_0_countries')
    reader = shpreader.Reader(shp)
    for n in reader.records() :
        if n.attributes['ADM0_A3'] == iso: 
            ax.add_geometries(n.geometry, ccrs.PlateCarree(), facecolor=clr, 
                              alpha = 1.00, linewidth =0.01, edgecolor = "white",
                              label=n.attributes['ADM0_A3']) 
    return ax

iso3 = ['CAN', 'MEX']

for value in iso3 :
    area(ax, value, "white")

for ax in p.axes.flat:
    gl = ax.gridlines(crs=ccrs.PlateCarree(), linewidth=0.01, color='w', alpha=0, linestyle='-', 
                      draw_labels=False,dms=True, x_inline=False, y_inline=False)
    ax.add_feature(cfeature.COASTLINE, color='k')
    ax.add_feature(cfeature.STATES, linewidth=0.1, zorder=3)
    ax.add_feature(cfeature.OCEAN, color='white', zorder=2)
    ax.add_feature(cfeature.LAKES, color='white', zorder=1.5)
    ax.add_feature(cfeature.BORDERS, color='grey')
    ax.set_extent([-125, -75, 25.3, 46.5], crs=ccrs.PlateCarree())
    ax.outline_patch.set_visible(False)
    gl.xlabels_top = False
    gl.ylabels_right = False
    area(ax, value, "white")

#Adjust colorbar and subplots
plt.subplots_adjust(wspace=0.05, hspace=-0.75)
p.add_colorbar(orientation='vertical', pad=0.04, shrink=0.4, aspect=25, label='Mean Biomass ($kg,ha^{-1}$)')

This is the output: Maps of crop biomass for two irrigations (1 and 2) and four scen_new levels (0,1,2,3)

question from:https://stackoverflow.com/questions/65642250/re-order-the-sequence-of-rows-plotted-in-a-figure-with-subplots-using-xarray-dat

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...