Skip to content
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

LogNorm colormap doesn't work correctly #25

Closed
bradyrx opened this issue Jul 9, 2019 · 2 comments
Closed

LogNorm colormap doesn't work correctly #25

bradyrx opened this issue Jul 9, 2019 · 2 comments
Labels

Comments

@bradyrx
Copy link
Collaborator

bradyrx commented Jul 9, 2019

Using the LogNorm colormap argument in, e.g., pcolormesh doesn't work correctly in proplot.

Data link: http://s000.tinyupload.com/index.php?file_id=02946064711977690771

With matplotlib:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib.colors as colors

ds = xr.open_dataset('chl_cal.nc')

f, ax = plt.subplots(subplot_kw=dict(projection=ccrs.PlateCarree()))
p = ax.pcolormesh(ds.lon, ds.lat, ds, norm=colors.LogNorm(vmin=ds.min(), vmax=ds.max()))
plt.colorbar(p)

Screen Shot 2019-07-09 at 4 50 54 PM

With proplot:

f, ax = plot.subplots(proj='pcarree', tight=False, axwidth=6, colorbar='r')
p = ax.pcolormesh(ds.lon, ds.lat, ds.values, norm=colors.LogNorm(vmin=ds.min(), vmax=ds.max()))
ax.set_extent([-140, -105, 20, 50])
f.rightpanel.colorbar(p)

Screen Shot 2019-07-09 at 4 51 28 PM

Here's a workaround, but I think using LogNorm is preferred so it handles the labeling and all that for you. Now this is of course in actual log10 units.

# Just log-transform before
ds = np.log10(ds)
f, ax = plot.subplots(proj='pcarree', tight=False, axwidth=6, colorbar='r')
p = ax.pcolormesh(ds.lon, ds.lat, ds.values,)
ax.set_extent([-140, -105, 20, 50])
f.rightpanel.colorbar(p)

Screen Shot 2019-07-09 at 4 52 33 PM

@lukelbd
Copy link
Collaborator

lukelbd commented Jul 10, 2019

Thanks for posting this, it triggered a much-needed cleanup of cmap_wrapper (commit 42ec0be). Details are in the cmap_wrapper documentation. Try this new example:

import proplot as plot
import xarray as xr
import numpy as np
%matplotlib inline
ds = xr.open_dataarray('/Users/ldavis/tmp/chl_cal.nc')
f, ax = plot.subplots(proj='pcarree', tight=False, axwidth=6, colorbar='r')
p = ax.pcolormesh(ds.lon, ds.lat, ds.values, norm='log', levels=15)
# p = ax.contourf(ds.lon, ds.lat, ds.values, norm='log', levels=15) # also works!
ax.set_extent([-140, -105, 20, 50])
f.rightpanel.colorbar(p)

Now the pcolor level selection behavior is like the matplotlib contourf selection behavior -- boundaries are "nice" by default (uses matplotlib.ticker.MaxNLocator). And now you can have nicely-spaced levels in plots with logarithmically-scaled colormaps, which was always really tricky to do in matplotlib. Note that setting vmin and vmax to the data minimum and maximum isn't really necessary.

By the way it looks like you still have that issue with thin fonts? If you run

rm ~/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/Helvetica*

and restart IPython it should fix the issue. Font control in matplotlib is really tricky, for now I have to add font files directly to the matplotlib data folder but am considering changing this.

Also keep in mind one of the big things with ProPlot is you no longer have to refer to these verbose class names (e.g. matplotlib.colors.LogNorm) -- everything is registered just like axis scale names or basemap projection names, including colormap Normalizers, Locators, Formatters, and cartopy projections. This is less verbose, fewer import statements, etc. This point is now emphasized in the README.

And now I'm going to sleep.

@lukelbd lukelbd closed this as completed Jul 10, 2019
@bradyrx
Copy link
Collaborator Author

bradyrx commented Jul 10, 2019

This is awesome, thanks @lukelbd. Resulting plot from this update is going on my poster I'm working on. Thanks also for the cmap_wrapper documentation. I didn't realize I could just pass arguments directly from there into pcolor, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants