-
Notifications
You must be signed in to change notification settings - Fork 149
plot NaNs in gray color and use log color bar #929
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
Conversation
|
@ValentinGebhart Thank you for that contribution. Can you share an example and compare the resulting plots before and after your changes? |
This is an example of plotting the return periods of a hazard object where there are some NaNs (because the centroid had never seen the given threshold intensity, so the return period is given as NaN), and some centroids are removed (left bottom corner). This is the code: import numpy as np centroids_mask = np.array( return_periods, label, column_label = haz_tc_fl.local_return_period([30, 40]) from climada.util.plot import plot_from_gdf Note that if the value range of the hazard return periods was more than two orders of magnitude (without having zeros), the color scale would also be logarithmic in the new plots |
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.
I very much like the overall contribution, but I have to say I dislike the approach. Calling griddata again on basically the same data, casting to bool, plotting with a weird colormap...
I think the same thing can be achieved much easier, using all the tools of Matplotlib. You can set "bad" and "over/under" colors for a colormap. Choosing the right vmin should then give you the expected outcome with a single call to pcolormesh
# ...
if "norm" in kwargs:
min_value = kwargs["norm"].vmin
vmin = None # We will pass norm
else:
min_value = np.nanmin(array_im)
vmin = kwargs.pop("vmin", min_value)
grid_im = griddata(
(coord[:, 1], coord[:, 0]),
array_im,
(grid_x, grid_y),
fill_value=min_value-1, # Values outside the grid
)
# ...
cmap = plt.get_cmap(kwargs.pop("cmap", "viridis"))
cmap.set_bad("gray") # For NaNs and infs
cmap.set_under("white", alpha=0) # For values below vmin
axis.pcolormesh(
grid_x - mid_lon,
grid_y,
np.squeeze(grid_im),
transform=proj,
cmap=cmap,
vmin=vmin,
**kwargs
)
climada/util/plot.py
Outdated
| gdf = gdf[['geometry', *[col for col in gdf.columns if col != 'geometry']]] | ||
| gdf_values = gdf.values[:,1:].T |
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.
See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html
| gdf = gdf[['geometry', *[col for col in gdf.columns if col != 'geometry']]] | |
| gdf_values = gdf.values[:,1:].T | |
| gdf_values = gdf.drop(columns="geometry") |
climada/util/plot.py
Outdated
| ): | ||
| kwargs.update( | ||
| {'norm': mpl.colors.LogNorm( | ||
| vmin=gdf.values[:,1:].min(), vmax=gdf.values[:,1:].max() |
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.
| vmin=gdf.values[:,1:].min(), vmax=gdf.values[:,1:].max() | |
| vmin=gdf_values.min(), vmax=gdf_values.max() |
|
Thanks for the advice! I agree that the way you describe is easier. I implemented and tested it (example plots from above didn't change), with a small modification for the case of the log colorscale (min_value - 1 did not seem to work, so I used min_value/2). |
peanutfun
left a comment
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.
Okay, this is looking much better now, thanks for the update! I have a few nitpicky suggestions still 🙈 We can merge once these are resolved!


Changes proposed in this PR:
geo_im_from_array, NaN values in the data will be plotted in gray. Before, NaN value were not plotted (i.e. transparent), making them indistinguishable from plot regions for which there is no data (no centroids).plot_from_gdf, the colorbar with will be shown on a logarithmic scale if a) the gdf is about return periods or impacts, b) there are no zeros in the data, c) the span of the data's values are at least two orders of magnitudePR Author Checklist
develop)PR Reviewer Checklist