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

Fix contourf set under #3601

Merged
merged 17 commits into from
Feb 24, 2020
Merged

Conversation

johnomotani
Copy link
Contributor

@johnomotani johnomotani commented Dec 6, 2019

Copies the cmap._rgba_bad, cmap._rgba_under, and cmap._rgba_over values to new_cmap, in case they have been set to non-default values. Allows the user to customize plots more by using matplotlib methods on a cmap before passing as an argument to xarray's plotting methods. Previously these settings were overridden by defaults when creating the cmap actually used to make the plot.

I'm not a fan of copying attributes one-by-one like this, but I guess this is an issue with matplotlib's API, unless there's a nicer way to convert a cmap to a discrete cmap than mpl.colors.from_levels_and_colors().

Copies the cmap._rgba_bad, cmap._rgba_under, and cmap._rgba_over values
to new_cmap, in case they have been set to non-default values. Allows
the user to customize plots more by using matplotlib methods on a cmap
before passing as an argument to xarray's plotting methods. Previously
these settings were overridden by defaults when creating the cmap
actually used to make the plot.
If the input cmap is a str, getting _rgba_bad, _rgba_under, or
_rgba_over attributes from it will fail. To fix this, provide defaults
taken from new_cmap.
Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the late review @johnomotani. Thanks for contributing!

Make one unit test test all three properties, rather than having a
separate test for each.
@johnomotani
Copy link
Contributor Author

Not sure why some tests are failing... The ones I can see are to do with CFTime. I can't see a relation to the changes in this PR??

@keewis
Copy link
Collaborator

keewis commented Jan 22, 2020

that's #3673, you can ignore these upstream-dev failures

When modifying a colormap, we only want to preserve the under and over
colors of the original colormap if they were explicitly set by the user.
In _build_discrete_cmap this makes no difference, as the new_cmap
returned by mpl.colors.from_levels_and_colors has the same minimum and
maximum colors as its input, so the default under and over colors would
not change anyway and could be copied regardless. However, for clarity
and in case the same pattern is needed in future elsewhere, it is nicer
to add a checks for: whether the under color is the same as cmap(0),
only setting under for new_cmap if it is not; and whether the over color
is the same as cmap(cmap.N - 1), only setting over for new_cmap if it is
not.
Copy link
Collaborator

@mathause mathause left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to see this in! An entry to whats new is still missing.

@mathause
Copy link
Collaborator

mathause commented Feb 4, 2020

I think (some of) the test failures are unrelated (test_seaborn_palette_as_cmap), see #3747

@johnomotani johnomotani force-pushed the fix-contourf-set_under branch from 966fa4c to 55fa277 Compare February 4, 2020 15:44
cmaps contain tuples member variables, so safer to deepcopy instead of
just copy to make sure we never change the copied variable.
Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @johnomotani . I've left some minor comments. This is looking good otherwise.

Set vmin and vmax so that _build_discrete_colormap is called with
extend='both'. extend is passed to mpl.colors.from_levels_and_colors(),
which returns a result with sensible under and over values if
extend='both', but not if extend='neither' (but if extend='neither' the
under and over values would not be used because the data would all be
within the plotted range)
@johnomotani johnomotani force-pushed the fix-contourf-set_under branch from 5bb0247 to 061ddf2 Compare February 5, 2020 18:07
@johnomotani
Copy link
Contributor Author

I've added an extra test that checks the right thing happens when we do not change the bad, under or over colors. That test needs to set vmin and vmax to get sensible defaults for the under and over colors: without vmin or vmax, mpl.colors.from_levels_and_colors() is called with extend = 'neither'; then it sets _rgba_under and _rgba_over in its result to (0.0, 0.0, 0.0, 0.0) (but these values can never be used, because with no vmin or vmax there can be no data outside of the plotted range).

I think this is ready now. The unit test failures on my local computer appear to be unrelated to this PR (see earlier references to #3673 by @keewis and #3747 by @mathause).

…under

* upstream/master: (71 commits)
  Optimize isel for lazy array equality checking (pydata#3588)
  pin msgpack (pydata#3793)
  concat now handles non-dim coordinates only present in one dataset (pydata#3769)
  Add new h5netcdf backend phony_dims kwarg (pydata#3753)
  always use dask_array_type for isinstance calls (pydata#3787)
  allow formatting the diff of ndarray attributes (pydata#3728)
  Pint support for variables (pydata#3706)
  Format issue template comment as md comment (pydata#3790)
  Avoid running test_open_mfdataset_list_attr without dask (pydata#3780)
  remove seaborn.apionly compatibility (pydata#3749)
  Python 3.8 CI (pydata#3727)
  PKG: Explicitly add setuptools dependency (pydata#3628)
  update whats-new
  Typo in Universal Functions section (pydata#3663)
  Release v0.15.0
  fix setup.cfg
  Documentation fixes (pydata#3732)
  Remove extra && in PR template (pydata#3730)
  Remove garbage text inserted in DASK_LICENSE (pydata#3729)
  Avoid unsafe use of pip (pydata#3726)
  ...
@dcherian dcherian merged commit b14eea2 into pydata:master Feb 24, 2020
@dcherian
Copy link
Contributor

Thanks @johnomotani

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

Successfully merging this pull request may close these issues.

cmap.set_under() does not work as expected
4 participants