Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Proplot colormaps cause issues with default xarray plotting #2

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

Closed
bradyrx opened this issue Jan 9, 2019 · 7 comments
Closed

Proplot colormaps cause issues with default xarray plotting #2

bradyrx opened this issue Jan 9, 2019 · 7 comments
Labels

Comments

@bradyrx
Copy link
Collaborator

bradyrx commented Jan 9, 2019

Quick plots with xarray (e.g., ds.plot()) use 'viridis' as the default sequential colormap and 'RdBu' as the default diverging colormap.

Thus, if one is to use ds.plot() with negative and positive values, an error is thrown because 'RdBu' doesn't exist once proplot is imported. For whatever reason, it seems like the 'RdBu' convention from matplotlib (https://matplotlib.org/examples/color/colormaps_reference.html) was switched to 'BuRd' in proplot.

Some other cases here are 'RdYlBu' from matplotlib becomes 'BuYlRd'. I imagine reordering of these strings should be avoided so that errors like this aren't thrown from outside packages. Perhaps simple aliases can be made to take either BuRd or RdBu.

@lukelbd
Copy link
Collaborator

lukelbd commented Jan 9, 2019

Thanks for the heads up, have never used the built-in xarray plotting commands.

Indeed I switched the names for some default colormaps because I thought 'RdBu' was a silly convention (99.9% of the time blue is "small/negative" and red is "large/positive"). Compatibility breaks didn't occur to me. I think I'll replace the colormap dictionary (stored in matplotlib.cm.cmap_d) with a new class that handles these naming issues -- total case insensitivity, and e.g. equivalency of 'RdBu_r' and 'BuRd'. Shouldn't take very long.

@bradyrx
Copy link
Collaborator Author

bradyrx commented Jan 9, 2019

I completely agree with your thinking (and understood why you did it when I saw it). I imagine this compatibility issue would extend to any library that default plots a certain colormap like xarray.

@lukelbd
Copy link
Collaborator

lukelbd commented Jan 9, 2019

Decided to just tackle this right now since I'd been meaning to do something similar anyway. Latest commit should satisfy the behavior I described (hopefully) without any side-effects, using a (probably overkill) fancy new class called _CmapDict. See:

a6af853

Try this example (after updating to the latest version):

f, axs = plot.subplots(ncols=6, axwidth=2, innercolorbars='b', innercolorbars_kw={'hspace':0.2})
for i,cmap in enumerate(('ColdHot', 'HotCold', 'HotCold_r', 'RdYlGn', 'GnYlRd', 'GnYlRd_r')):
    m = axs[i].contourf(np.random.rand(10,10), cmap=cmap)
    axs[i].bottompanel.colorbar(m)

Let me know if you discover any new issues and will re-open.

@lukelbd lukelbd closed this as completed Jan 9, 2019
@lukelbd
Copy link
Collaborator

lukelbd commented Jan 9, 2019

(Accidentally left a print statement in there; latest commit will have deleted it.)

@bradyrx
Copy link
Collaborator Author

bradyrx commented Jan 10, 2019

@lukelbd, this doesn't seem to solve it on my end. Note that I did upgrade the package to the most recent commits.

Using your example I get the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-27-9b7beea59934> in <module>
----> 1 f, axs = plot.subplots(ncols=6, axwidth=2, innercolorbars='b', innercolorbars_kw={'hspace':0.2})
      2 for i,cmap in enumerate(('ColdHot', 'HotCold', 'HotCold_r', 'RdYlGn', 'GnYlRd', 'GnYlRd_r')):
      3     m = axs[i].contourf(np.random.rand(10,10), cmap=cmap)
      4     axs[i].bottompanel.colorbar(m)

~/Desktop/proplot/proplot/subplots.py in subplots(array, ncols, nrows, rowmajor, emptycols, emptyrows, tight, auto_adjust, rcreset, silent, span, share, spanx, spany, sharex, sharey, innerpanels, innercolorbars, innerpanels_kw, basemap, proj, projection, proj_kw, projection_kw, **kwargs)
    262 
    263     # Create gridspec for outer plotting regions (divides 'main area' from side panels)
--> 264     figsize, offset, subplots_kw, gridspec_kw = _gridspec_kwargs(nrows, ncols, **kwargs)
    265     row_offset, col_offset = offset
    266     gs = FlexibleGridSpec(**gridspec_kw)

TypeError: _gridspec_kwargs() got an unexpected keyword argument 'innercolorbars_kw'

Running ds.plot() returns the same issue as before. Interestingly, I tried to run import proplot.colortools to just check that the new Class was showing up, and get the following error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-30-bba849ec6737> in <module>
----> 1 import proplot.colortools

ModuleNotFoundError: No module named 'proplot.colortools'

This happens with all submodules. Not sure if you have some lock on importing submodules directly? Although not sure how one would do that. Note that import proplot works just fine, so this is isolated to submodules.

@lukelbd lukelbd reopened this Jan 10, 2019
@lukelbd
Copy link
Collaborator

lukelbd commented Jan 10, 2019

Hmm, innercolorbars_kw (which is just an alias for innerpanels_kw) should have been added by latest commit.

The second example doesn't work because I just use the individual files to organize things (__init__.py contains from .colortools import *; from .axistools import *; etc.. Wanted user to have access to all commands at the top-level (without submodules).

Try accessing plot._CmapDict; does it show anything? If not, maybe try uninstalling/re-installing. I pushed the changes.

I wonder if when you pip install from a git URL, if the version number in setup.py hasn't changed (which it hasn't), pip just won't update the repo?

@bradyrx
Copy link
Collaborator Author

bradyrx commented Jan 10, 2019

It works now... can't tell you what changed. I installed via pip and your example as well as ds.plot() on anomalies works.

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