Skip to content

Supplying a "non-cartopy" axis to Hazard.plot_intensity() fails #1044

@spjuhel

Description

@spjuhel

Describe the bug
A clear and concise description of what the bug is.

Attempting to plot hazard intensity in a custom axis via the axis= argument of plot_intensity() raises an AttributeError.

To Reproduce
Steps to reproduce the behavior/error:

import numpy as np
import matplotlib.pyplot as plt
from climada.hazard import Hazard
from climada.util.constants import HAZ_DEMO_FL

# to hide the warnings
import warnings

warnings.filterwarnings("ignore")

# read intensity from raster file HAZ_DEMO_FL and set frequency for the contained event
haz_ven = Hazard.from_raster(
    [HAZ_DEMO_FL], attrs={"frequency": np.ones(1) / 2}, haz_type="FL"
)
haz_ven.check()

# The masked values of the raster are set to 0
# Sometimes the raster file does not contain all the information, as in this case the mask value -9999
# We mask it manuall and plot it using plot_intensity()
haz_ven.intensity[haz_ven.intensity == -9999] = 0


fig, ax = plt.subplots(figsize=(12,6))
haz_ven.plot_intensity(
    1, smooth=False, axis=ax
)  # if smooth=True (default value) is used, the computation time might increase

# per default the following attributes have been set
print("event_id: ", haz_ven.event_id)
print("event_name: ", haz_ven.event_name)
print("date: ", haz_ven.date)
print("frequency: ", haz_ven.frequency)
print("orig: ", haz_ven.orig)
print("min, max fraction: ", haz_ven.fraction.min(), haz_ven.fraction.max())

Raises following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[58], line 24
     20 haz_ven.intensity[haz_ven.intensity == -9999] = 0
     23 fig, ax = plt.subplots(figsize=(12,6))
---> 24 haz_ven.plot_intensity(
     25     1, smooth=False, axis=ax
     26 )  # if smooth=True (default value) is used, the computation time might increase
     28 # per default the following attributes have been set
     29 print("event_id: ", haz_ven.event_id)

File [~/miniforge3/envs/climada6/lib/python3.11/site-packages/climada/hazard/plot.py:144](http://localhost:8888/lab/tree/Repos/interoperability_sandbox/CLIMADA/miniforge3/envs/climada6/lib/python3.11/site-packages/climada/hazard/plot.py#line=143), in HazardPlot.plot_intensity(self, event, centr, smooth, axis, adapt_fontsize, **kwargs)
    142     if isinstance(event, str):
    143         event = self.get_event_id(event)
--> 144     return self._event_plot(
    145         event,
    146         self.intensity,
    147         col_label,
    148         smooth,
    149         crs_epsg,
    150         axis,
    151         adapt_fontsize=adapt_fontsize,
    152         **kwargs,
    153     )
    154 if centr is not None:
    155     if isinstance(centr, tuple):

File [~/miniforge3/envs/climada6/lib/python3.11/site-packages/climada/hazard/plot.py:277](http://localhost:8888/lab/tree/Repos/interoperability_sandbox/CLIMADA/miniforge3/envs/climada6/lib/python3.11/site-packages/climada/hazard/plot.py#line=276), in HazardPlot._event_plot(self, event_id, mat_var, col_name, smooth, crs_espg, axis, figsize, adapt_fontsize, **kwargs)
    274     array_val.append(im_val)
    275     l_title.append(title)
--> 277 return u_plot.geo_im_from_array(
    278     array_val,
    279     self.centroids.coord,
    280     col_name,
    281     l_title,
    282     smooth=smooth,
    283     axes=axis,
    284     figsize=figsize,
    285     proj=crs_espg,
    286     adapt_fontsize=adapt_fontsize,
    287     **kwargs,
    288 )

File [~/miniforge3/envs/climada6/lib/python3.11/site-packages/climada/util/plot.py:454](http://localhost:8888/lab/tree/Repos/interoperability_sandbox/CLIMADA/miniforge3/envs/climada6/lib/python3.11/site-packages/climada/util/plot.py#line=453), in geo_im_from_array(array_sub, coord, var_name, title, proj, smooth, shapes, axes, figsize, adapt_fontsize, **kwargs)
    452         grid_im = np.flip(grid_im, 1)
    453     grid_im = np.resize(grid_im, (height, width, 1))
--> 454 axis.set_extent(
    455     (extent[0] - mid_lon, extent[1] - mid_lon, extent[2], extent[3]), crs=proj
    456 )
    458 # Add coastline to axis
    459 if shapes:

AttributeError: 'Axes' object has no attribute 'set_extent'

Expected behavior
A clear and concise description of what you expected to happen.

Either the function should return a plot anyway, or at least the error message and the docstring should be more explicit about the fact that the axis needs to be created with cartopy.

Climada Version: 6.0.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions