Search code examples
pythonplotprojectioncartopycontourf

Cartopy contourf and coastlines but the coastlines aren't showing


I'm trying to plot precipitation data of a region downloaded from GESC Earthdata archive using cartopy's contourf and coastlines but for some reason only the cmap of data is showing and the coastlines are undershadowed by the data. Here's my code for the plotting.

plt.clf() #clear figure before
fig=plt.figure()

ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=100.0, globe=None))
ax.set_extent([95,106,0,9]) # lon_left, lon_right, lat_below, lat_upper
ax.coastlines("10m", alpha=0.1) # avail:110m, 50m, 10m..... '10m' is better resolution than default
ax.gridlines(linewidths=0.01, draw_labels=True, alpha= 0.3)
ax.xlocator = mticker.FixedLocator(np.arange(95.,106.,0.5))
ax.ylocator = mticker.FixedLocator(np.arange(0.,9.,0.5))

x,y=np.meshgrid(lat,lon) 
clevs = np.arange(5,800,50)
cs = ax.contourf(y, x, finalmean,  levels=clevs, extend="max", cmap='viridis_r', transform=ccrs.PlateCarree())
# draw legend -------------
#cax = fig.add_axes([0.1,0.018,0.8,0.05], projection=ccrs.PlateCarree()) #plot cb at the bottom [left, bottom, width, height] 
#aa=fig.colorbar(cs,cax=cax,orientation='horizontal')
aa.set_label('Rainfall (mm)')
#plt.savefig("trmm_clim_mo_1.jpg", bbox_inches='tight')
plt.show()

Here's my output

output

I've also specified the "transform" argument in the plt.contourf as advised by those who'd encountered this same problem but to no avail. If anyone can point out what I'm missing/wrong with my code, that'd be very much appreciated.


Solution

  • Try using the matplotlib kwarg zorder to set which layers plot on top of each other. In you example:

    ax.coastlines("10m", alpha=0.1, zorder=3)

    should do. Objects with higher zorder plot on top of those with lower zorder.

    Alternatively, add your coastlines after the call to contourf. By default, layers you add later get added on top of the predecessors.