Skip to content

Commit 3ddc5d9

Browse files
adapted NaN plot handling and include legend
1 parent 5aeebea commit 3ddc5d9

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

climada/util/plot.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import cartopy.crs as ccrs
3838
import geopandas as gpd
3939
import matplotlib as mpl
40+
import matplotlib.patches as mpatches
4041
import matplotlib.pyplot as plt
4142
import numpy as np
4243
import requests
@@ -420,8 +421,7 @@ def geo_im_from_array(
420421

421422
# prepare colormap
422423
cmap = plt.get_cmap(kwargs.pop("cmap", CMAP_RASTER))
423-
cmap.set_bad("gainsboro") # For NaNs and infs
424-
cmap.set_under("white", alpha=0) # For values below vmin
424+
cmap.set_under("white") # For values below vmin
425425

426426
# Generate each subplot
427427
for array_im, axis, tit, name in zip(
@@ -470,6 +470,15 @@ def geo_im_from_array(
470470
cmap=cmap,
471471
**kwargs,
472472
)
473+
# handle NaNs in griddata
474+
color_nan = "gainsboro"
475+
if np.any(np.isnan(grid_im)):
476+
no_data_patch = mpatches.Patch(color=color_nan, label="NaN")
477+
axis.legend(
478+
handles=[no_data_patch] + axis.get_legend_handles_labels()[0],
479+
loc="lower right",
480+
)
481+
axis.set_facecolor(color_nan)
473482
cbar = plt.colorbar(img, cax=cbax, orientation="vertical")
474483
cbar.set_label(name)
475484
axis.set_title("\n".join(wrap(tit)))

0 commit comments

Comments
 (0)