-
Notifications
You must be signed in to change notification settings - Fork 104
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
Automatically apply diverging colormaps and add robust=True
option
#78
Comments
(1) Yes there is. There are two options: ax.[contourf|pcolor|...](data, symmetric=True) # auto-select levels symmetric about zero
ax.[contourf|pcolor|...](data, norm='midpoint') # warp the colorbar so that zero falls on the central level, even if asymmetric (2) Could you post an example? All |
Both of these are features of xarray's .plot() API. So you can access them just by using the xarray API, which wraps matplotlib. So all of proplot's features also get incorporated. Well , I guess not the plotting wrappers but at least formatters and so on. |
@bradyrx So xarray automatically selects a diverging map and applies the equivalent of |
Oh @mickaellalande do you mean that Currently |
I didn't know about the symmetric keyword, but I imagine that is what is happening. If you pass in values that range from neg to pos, it centers on zero and applies a diverging colormap.
That's what I suspect. I think @mickaellalande is passing @lukelbd I would just scroll through that |
Sorry not to have been precise enough and without example, but I guess you understood what I meant. So I am going to illustrate what I was meaning with xarray/cartopy and then with ProPlot (lot of figures coming up!): Data example: season_clim_diff.zip
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
season_clim_diff = xr.open_dataarray('season_clim_diff.nc')
season = "SON"
plt.figure(figsize=(10,4))
ax = plt.axes(projection=ccrs.PlateCarree(180))
season_clim_diff.sel(season=season).plot.contourf(ax=ax, transform=ccrs.PlateCarree())
ax.coastlines()
ax.set_global()
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')
gl.xlabels_top = False
gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER As you can see, by default when we write
season_clim_diff.sel(season=season).plot.contourf(ax=ax, transform=ccrs.PlateCarree(), robust=True)
It is usually convenient. Except when it extends only one side of the color bar (like in this example) and makes the color bar not symmetric anymore (shifted of one bin). I don't know if it is possible to fix this... I usually put
import proplot as plot
season = "SON"
f, axs = plot.subplots(proj='cyl', proj_kw={'lon_0':180}, width=6)
m = axs[0].contourf(season_clim_diff.sel(season=season))
f.colorbar(m, label="Snow Area Fraction [%]")
axs.format(geogridlinewidth=0.5, geogridcolor='gray8', geogridalpha=0.5, labels=True,
coast=True, suptitle=season+" snow cover bias", ocean=True, oceancolor='gray4') That was what I was meaning, it would be nice to have automatically a diverging colormap centered on 0 (symmetric or not?). I didn't know about these options ( m = axs[0].contourf(season_clim_diff.sel(season=season), cmap='ColdHot', symmetric=True) or m = axs[0].contourf(season_clim_diff.sel(season=season), cmap='ColdHot', norm='midpoint') And if I try to reproduce the robust colormap with xarray/cartopy, I put some levels by hand: m = axs[0].contourf(season_clim_diff[0], cmap='ColdHot', levels=np.arange(-45,50,5)) And with the m = axs[0].contourf(season_clim_diff[0], cmap='ColdHot', levels=np.arange(-45,50,5), extend='max', norm='midpoint') A bit long, but I hope all these examples can help to improve proplot! (and/or makes me discover other option that I don't know). Thanks again for the quit reply and involvement! |
@mickaellalande Thanks for the examples! Yes I'll add a feature where if the minimum negative (maximum positive) value is at least 10% or 20% of the magnitude of the maximum positive (minimum negative) value, a diverging colormap is automatically selected. Or could use percentiles for this test. And maybe will allow this threshold to be configurable. Not sure what the deal is with that gridliner issue, will check it out. |
Btw proplot has a |
Thanks, I find this also more convenient! |
robust=True
option
These features are finally on master. Below is an example of the import proplot as pplt
import numpy as np
fig, axs = pplt.subplots(ncols=2)
N = 10
data = np.random.rand(N, N)
data.flat[0] = 100.0
for i, ax in enumerate(axs):
ax.pcolor(data, robust=bool(i))
ax.format(title='Robust ' + ('on' if i else 'off')) And here's an example of the automatic diverging colormap selection. A diverging colormap is picked as follows:
I've also added a whole system for selecting default "sequential", "diverging", "qualitative", and "cyclic" colormaps with corresponding rc settings and keyword arguments that can be passed to plot commands (e.g., import proplot as pplt
import numpy as np
fig, axs = pplt.subplots(ncols=2)
N = 10
data = np.random.rand(N, N) * 10
for i, ax in enumerate(axs):
ax.pcolor(data - i * 5, colorbar='b')
ax.format(title='Diverging ' + ('on' if i else 'off')) |
Is there any way to have an automatic scaling of the colorbar when 0 is present, like the default behavior of Cartopy that center the colorbar on 0 and automatically use a diverging colormap?
And in addition, is that possible to use the option
robust=True
of Cartopy? When I try to use it, it tells me that it is not recognized.The text was updated successfully, but these errors were encountered: