简体   繁体   中英

How to set a single title for facetted xarray plot?

I need to set a title for multiple plots each of which is drawn with Basemap.

The souce data is an xarray's DataArray object. The code uses DataArray.plot() to draw multiple plots on a single figure.

p = da_monthly_slice.plot(levels=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], cmap=plt.cm.Reds, x='longitude', y='latitude', col='time', col_wrap=3)

for i, ax in enumerate(p.axes.flatten()):
    ax.set_title('Month: %d' % i)
    ax.set_xlabel('Longitude')
    ax.set_ylabel('Latitude')
    map = Basemap(llcrnrlat=-39.2,urcrnrlat=-33.9,llcrnrlon=140.8,urcrnrlon=150.0,resolution='i',ax=ax)
    map.readshapefile(shapefile1, 'CFA_DISTRICT_BODY', linewidth=0.5)

fig = plt.figure()
st = fig.suptitle("Number of days for each month meeting the criteria in 2017", fontsize="large")

plt.show()

This caused two figures drawn separately. One is for the multiple plots while the other one contains the title only.

How to make the title to show on the top of the multiple plots on the same figure?

fig = plt.figure() creates a new figure. Remove that line.

You can use plt.suptitle(..) to create a title in the current figure.
Or you get the figure from the FacetGrid , it should be p.fig .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM