Skip to content

Commit c3b5376

Browse files
authored
Merge pull request #1073 from CLIMADA-project/feature/plot_intensity_efficiency
speed-up the function to add country borders and coastlines
2 parents b2d9aa8 + be97efc commit c3b5376

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Removed:
2222
- `ImpactFunc` and `ImpactFuncSet` now support equality comparisons via `==` [#1027](https://github.com/CLIMADA-project/climada_python/pull/1027)
2323

2424
### Changed
25-
25+
- Changed the default mask_distance in `util.plot.geo_im_from_array` to 0.03 to avoid white gaps in gridded hazard data with comparably low resolution (>80 centroids per axis) [#1073](https://github.com/CLIMADA-project/climada_python/pull/1073)
26+
- Increased speed of `util.plot.add_shapes` by avoiding for loops, substantially speeding up `Hazard.plot_intensity` and other functions. [#1073](https://github.com/CLIMADA-project/climada_python/pull/1073)
2627
- `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)
2728
- World Bank indicator data is now downloaded directly from their API via the function `download_world_bank_indicator`, instead of relying on the `pandas-datareader` package [#1033](https://github.com/CLIMADA-project/climada_python/pull/1033)
2829
- `Exposures.write_hdf5` pickles geometry data in WKB format, which is faster and more sustainable. [#1051](https://github.com/CLIMADA-project/climada_python/pull/1051)

climada/engine/impact.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ def plot_rp_imp(
11781178
return_periods=(25, 50, 100, 250),
11791179
log10_scale=True,
11801180
axis=None,
1181-
mask_distance=0.01,
1181+
mask_distance=0.03,
11821182
kwargs_local_exceedance_impact=None,
11831183
**kwargs,
11841184
):
@@ -1198,8 +1198,8 @@ def plot_rp_imp(
11981198
mask_distance: float, optional
11991199
Only regions are plotted that are closer to any of the data points than this distance,
12001200
relative to overall plot size. For instance, to only plot values
1201-
at the centroids, use mask_distance=0.01. If None, the plot is not masked.
1202-
Default is 0.01.
1201+
at the centroids, use mask_distance=0.03. If None, the plot is not masked.
1202+
Default is 0.03.
12031203
kwargs_local_exceedance_impact: dict
12041204
Dictionary of keyword arguments for the method impact.local_exceedance_impact.
12051205
kwargs : dict, optional

climada/hazard/plot.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def plot_rp_intensity(
4040
self,
4141
return_periods=(25, 50, 100, 250),
4242
axis=None,
43-
mask_distance=0.01,
43+
mask_distance=0.03,
4444
kwargs_local_exceedance_intensity=None,
4545
**kwargs,
4646
):
@@ -60,8 +60,8 @@ def plot_rp_intensity(
6060
mask_distance: float, optional
6161
Only regions are plotted that are closer to any of the data points than this distance,
6262
relative to overall plot size. For instance, to only plot values
63-
at the centroids, use mask_distance=0.01. If None, the plot is not masked.
64-
Default is 0.01.
63+
at the centroids, use mask_distance=0.03. If None, the plot is not masked.
64+
Default is 0.03.
6565
kwargs: optional
6666
arguments for pcolormesh matplotlib function used in event plots
6767
@@ -111,7 +111,7 @@ def plot_intensity(
111111
smooth=True,
112112
axis=None,
113113
adapt_fontsize=True,
114-
mask_distance=0.01,
114+
mask_distance=0.03,
115115
**kwargs,
116116
):
117117
"""Plot intensity values for a selected event or centroid.
@@ -138,8 +138,8 @@ def plot_intensity(
138138
mask_distance: float, optional
139139
Only regions are plotted that are closer to any of the data points than this distance,
140140
relative to overall plot size. For instance, to only plot values
141-
at the centroids, use mask_distance=0.01. If None, the plot is not masked.
142-
Default is 0.01.
141+
at the centroids, use mask_distance=0.03. If None, the plot is not masked.
142+
Default is 0.03.
143143
kwargs: optional
144144
arguments for pcolormesh matplotlib function
145145
used in event plots or for plot function used in centroids plots
@@ -181,7 +181,7 @@ def plot_fraction(
181181
centr=None,
182182
smooth=True,
183183
axis=None,
184-
mask_distance=0.01,
184+
mask_distance=0.03,
185185
**kwargs,
186186
):
187187
"""Plot fraction values for a selected event or centroid.
@@ -208,8 +208,8 @@ def plot_fraction(
208208
mask_distance: float, optional
209209
Relative distance (with respect to maximal map extent in longitude or latitude) to data
210210
points above which plot should not display values. For instance, to only plot values
211-
at the centroids, use mask_distance=0.01. If None, the plot is not masked.
212-
Default is None.
211+
at the centroids, use mask_distance=0.03. If None, the plot is not masked.
212+
Default is 0.03.
213213
kwargs: optional
214214
arguments for pcolormesh matplotlib function
215215
used in event plots or for plot function used in centroids plots
@@ -252,7 +252,7 @@ def _event_plot(
252252
axis=None,
253253
figsize=(9, 13),
254254
adapt_fontsize=True,
255-
mask_distance=0.01,
255+
mask_distance=0.03,
256256
**kwargs,
257257
):
258258
"""Plot an event of the input matrix.
@@ -277,8 +277,8 @@ def _event_plot(
277277
mask_distance: float, optional
278278
Only regions are plotted that are closer to any of the data points than this distance,
279279
relative to overall plot size. For instance, to only plot values
280-
at the centroids, use mask_distance=0.01. If None, the plot is not masked.
281-
Default is None.
280+
at the centroids, use mask_distance=0.03. If None, the plot is not masked.
281+
Default is 0.03.
282282
kwargs: optional
283283
arguments for pcolormesh matplotlib function
284284

climada/util/plot.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from textwrap import wrap
3636

3737
import cartopy.crs as ccrs
38+
import cartopy.feature as cfeature
3839
import geopandas as gpd
3940
import matplotlib as mpl
4041
import matplotlib.patches as mpatches
@@ -338,7 +339,7 @@ def geo_im_from_array(
338339
axes=None,
339340
figsize=(9, 13),
340341
adapt_fontsize=True,
341-
mask_distance=0.01,
342+
mask_distance=0.03,
342343
**kwargs,
343344
):
344345
"""Image(s) plot defined in array(s) over input coordinates.
@@ -373,8 +374,8 @@ def geo_im_from_array(
373374
mask_distance: float, optional
374375
Only regions are plotted that are closer to any of the data points than this distance,
375376
relative to overall plot size. For instance, to only plot values
376-
at the centroids, use mask_distance=0.01. If None, the plot is not masked.
377-
Default is 0.01.
377+
at the centroids, use mask_distance=0.03. If None, the plot is not masked.
378+
Default is 0.03.
378379
**kwargs
379380
arbitrary keyword arguments for pcolormesh matplotlib function
380381
@@ -725,24 +726,15 @@ def make_map(num_sub=1, figsize=(9, 13), proj=ccrs.PlateCarree(), adapt_fontsize
725726

726727
def add_shapes(axis):
727728
"""
728-
Overlay Earth's countries coastlines to matplotlib.pyplot axis.
729+
Overlay Earth's countries and coastlines to matplotlib.pyplot axis.
729730
730731
Parameters
731732
----------
732733
axis : cartopy.mpl.geoaxes.GeoAxesSubplot
733734
Cartopy axis
734-
projection : cartopy.crs projection, optional
735-
Geographical projection.
736-
The default is PlateCarree.
737735
"""
738-
shp_file = shapereader.natural_earth(
739-
resolution="10m", category="cultural", name="admin_0_countries"
740-
)
741-
shp = shapereader.Reader(shp_file)
742-
for geometry in shp.geometries():
743-
axis.add_geometries(
744-
[geometry], crs=ccrs.PlateCarree(), facecolor="none", edgecolor="dimgray"
745-
)
736+
axis.add_feature(cfeature.BORDERS.with_scale("10m"), edgecolor="dimgrey")
737+
axis.add_feature(cfeature.COASTLINE.with_scale("10m"), edgecolor="dimgrey")
746738

747739

748740
def _ensure_utf8(val):
@@ -1104,7 +1096,7 @@ def plot_from_gdf(
11041096
axis=None,
11051097
figsize=(9, 13),
11061098
adapt_fontsize=True,
1107-
mask_distance=0.01,
1099+
mask_distance=0.03,
11081100
**kwargs,
11091101
):
11101102
"""Plot several subplots from different columns of a GeoDataFrame, e.g., for
@@ -1130,8 +1122,8 @@ def plot_from_gdf(
11301122
mask_distance: float, optional
11311123
Relative distance (with respect to maximal map extent in longitude or latitude) to data
11321124
points above which plot should not display values. For instance, to only plot values
1133-
at the centroids, use mask_distance=0.01. If None, the plot is not masked.
1134-
Default is 0.01.
1125+
at the centroids, use mask_distance=0.03. If None, the plot is not masked.
1126+
Default is 0.03.
11351127
kwargs: optional
11361128
Arguments for pcolormesh matplotlib function used in event plots.
11371129

0 commit comments

Comments
 (0)