-
Notifications
You must be signed in to change notification settings - Fork 157
Add option in raster plot to crop around centroids #1047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ce2629e
45537e7
7635890
d256588
09af3ae
b0547bc
5ff4ddb
7d07334
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ | |
| from mpl_toolkits.axes_grid1 import make_axes_locatable | ||
| from rasterio.crs import CRS | ||
| from scipy.interpolate import griddata | ||
| from scipy.spatial import cKDTree | ||
| from shapely.geometry import box | ||
|
|
||
| import climada.util.coordinates as u_coord | ||
|
|
@@ -337,6 +338,7 @@ | |
| axes=None, | ||
| figsize=(9, 13), | ||
| adapt_fontsize=True, | ||
| mask_relative_distance=None, | ||
| **kwargs, | ||
| ): | ||
| """Image(s) plot defined in array(s) over input coordinates. | ||
|
|
@@ -368,6 +370,11 @@ | |
| adapt_fontsize : bool, optional | ||
| If set to true, the size of the fonts will be adapted to the size of the figure. Otherwise | ||
| the default matplotlib font size is used. Default is True. | ||
| mask_relative_distance: float, optional | ||
| Relative distance (with respect to maximal map extent in longitude or latitude) to data | ||
| points above which plot should not display values. For instance, to only plot values | ||
| at the centroids, use mask_relative_distance=0.01. If None, the plot is not masked. | ||
| Default is None. | ||
| **kwargs | ||
| arbitrary keyword arguments for pcolormesh matplotlib function | ||
|
|
||
|
|
@@ -448,6 +455,18 @@ | |
| (grid_x, grid_y), | ||
| fill_value=min_value, | ||
| ) | ||
| # Compute distance of each grid point to the nearest known point | ||
| if mask_relative_distance is not None: | ||
| tree = cKDTree(np.array((coord[:, 1], coord[:, 0])).T) | ||
| distances, _ = tree.query( | ||
| np.c_[grid_x.ravel(), grid_y.ravel()], | ||
| p=2, # for plotting squares and not sphere around centroids use p=np.inf | ||
| ) | ||
| threshold = ( | ||
| max(extent[1] - extent[0], extent[3] - extent[2]) | ||
| * mask_relative_distance | ||
| ) | ||
| grid_im[(distances.reshape(grid_im.shape) > threshold)] = min_value | ||
| else: | ||
| grid_x = coord[:, 1].reshape((width, height)).transpose() | ||
| grid_y = coord[:, 0].reshape((width, height)).transpose() | ||
|
|
@@ -477,7 +496,7 @@ | |
| ) | ||
| # handle NaNs in griddata | ||
| color_nan = "gainsboro" | ||
| if np.any(np.isnan(x) for x in grid_im): | ||
| if np.isnan(grid_im).any(): | ||
|
Comment on lines
-480
to
+498
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume, this is the fix for the bug?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the idea was that whenever grid_im has a NaN we want to include a small legend (because we plot nan values in gray).
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ValentinGebhart Thanks! You don't happen to have an example that you could point me to?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. This would be an example where we want the legend (see PR #1038): import numpy as np
from climada.hazard import Hazard
from climada.util import HAZ_DEMO_H5 # CLIMADA's Python file
haz_tc_fl = Hazard.from_hdf5(HAZ_DEMO_H5) # Historic tropical cyclones in Florida from 1990 to 2004
haz_tc_fl.check() # Use always the check() method to see if the hazard has been loaded correctly
centroids_mask = np.array(
[ (i + j > 10) for j in range(50) for i in range(50)]
)
haz_tc_fl.centroids = haz_tc_fl.centroids.select(sel_cen=centroids_mask)
haz_tc_fl.intensity = haz_tc_fl.intensity[:, -2434:]
return_periods, label, column_label = haz_tc_fl.local_return_period([30, 40])
from climada.util.plot import plot_from_gdf
plot_from_gdf(return_periods, colorbar_name=label, title_subplots=column_label)and here we do not want the legend but in the current develop version it would be printed as well. haz_tc_fl.plot_intensity(event=0) |
||
| no_data_patch = mpatches.Patch( | ||
| facecolor=color_nan, edgecolor="black", label="NaN" | ||
| ) | ||
|
|
@@ -1078,7 +1097,7 @@ | |
| ax.legend(bars, data.keys()) | ||
|
|
||
|
|
||
| def plot_from_gdf( | ||
| gdf: gpd.GeoDataFrame, | ||
| colorbar_name: str = None, | ||
| title_subplots: callable = None, | ||
|
|
@@ -1086,6 +1105,7 @@ | |
| axis=None, | ||
| figsize=(9, 13), | ||
| adapt_fontsize=True, | ||
| mask_relative_distance=None, | ||
| **kwargs, | ||
| ): | ||
| """Plot several subplots from different columns of a GeoDataFrame, e.g., for | ||
|
|
@@ -1108,6 +1128,11 @@ | |
| adapt_fontsize: bool, optional | ||
| If set to true, the size of the fonts will be adapted to the size of the figure. | ||
| Otherwise the default matplotlib font size is used. Default is True. | ||
| mask_relative_distance: float, optional | ||
| Relative distance (with respect to maximal map extent in longitude or latitude) to data | ||
| points above which plot should not display values. For instance, to only plot values | ||
| at the centroids, use mask_relative_distance=0.01. If None, the plot is not masked. | ||
| Default is None. | ||
| kwargs: optional | ||
| Arguments for pcolormesh matplotlib function used in event plots. | ||
|
|
||
|
|
@@ -1168,6 +1193,7 @@ | |
| axes=axis, | ||
| figsize=figsize, | ||
| adapt_fontsize=adapt_fontsize, | ||
| mask_relative_distance=mask_relative_distance, | ||
| **kwargs, | ||
| ) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧐 This parameter is really hard to describe. I've read it repeatedly and still need the example images to understand it.
For me it would be probably easier if it was written in a more "positive" way, something like
Though I'm not sure, whether it's better like that.
Then: don't meant to be fuzzy but the parameter name is a somewhat awkward, don't you think?
Is it not rather
relative_mask_distance? Or justmask_distance? Or what aboutplotting_range? (as in only plot stuff within this plotting range)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, I will change the description and the parameter name to
mask_distance. Thanks!