Skip to content

Commit 3cfbe05

Browse files
Merge pull request #1038 from CLIMADA-project/hotfix/plot_NaN_on_grid
Hotfix: plot NaNs in geo_im_from_array
2 parents 5aeebea + cc9b33a commit 3cfbe05

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Code freeze date: YYYY-MM-DD
1616
### Changed
1717
- `Hazard.local_exceedance_intensity`, `Hazard.local_return_period` and `Impact.local_exceedance_impact`, `Impact.local_return_period`, using the `climada.util.interpolation` module: New default (no binning), binning on decimals, and faster implementation [#1012](https://github.com/CLIMADA-project/climada_python/pull/1012)
1818
### Fixed
19+
- NaN plotting issues in `geo_im_from_array`[#1038](https://github.com/CLIMADA-project/climada_python/pull/1038)
1920

2021
### Deprecated
2122

climada/util/plot.py

Lines changed: 18 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
@@ -374,6 +375,11 @@ def geo_im_from_array(
374375
-------
375376
cartopy.mpl.geoaxes.GeoAxesSubplot
376377
378+
Notes
379+
-----
380+
Data points with NaN or inf are plotted in gray. White regions correspond to
381+
regions outside the convex hull of the given coordinates.
382+
377383
Raises
378384
------
379385
ValueError
@@ -420,8 +426,7 @@ def geo_im_from_array(
420426

421427
# prepare colormap
422428
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
429+
cmap.set_under("white") # For values below vmin
425430

426431
# Generate each subplot
427432
for array_im, axis, tit, name in zip(
@@ -470,6 +475,17 @@ def geo_im_from_array(
470475
cmap=cmap,
471476
**kwargs,
472477
)
478+
# handle NaNs in griddata
479+
color_nan = "gainsboro"
480+
if np.any(np.isnan(x) for x in grid_im):
481+
no_data_patch = mpatches.Patch(
482+
facecolor=color_nan, edgecolor="black", label="NaN"
483+
)
484+
axis.legend(
485+
handles=[no_data_patch] + axis.get_legend_handles_labels()[0],
486+
loc="lower right",
487+
)
488+
axis.set_facecolor(color_nan)
473489
cbar = plt.colorbar(img, cax=cbax, orientation="vertical")
474490
cbar.set_label(name)
475491
axis.set_title("\n".join(wrap(tit)))

0 commit comments

Comments
 (0)