Skip to content

FIX: be more careful about coercing unit-full containers to ndarray - #21884

Merged
QuLogic merged 2 commits into
matplotlib:mainfrom
tacaswell:fix_errorbar_units
Dec 10, 2021
Merged

QuLogic merged 2 commits into
matplotlib:mainfrom
tacaswell:fix_errorbar_units

Conversation

@tacaswell

Copy link
Copy Markdown
Member

PR Summary

closes #21669

Tested manually locally via:

import matplotlib.pyplot as plt
import unyt as un

un.matplotlib_support.enable()
fig, ax = plt.subplots()

x = un.unyt_array([1, 2], "cm")
y = un.unyt_array([3, 4], "kg")
yerr = un.unyt_array([0.1, 0.2], "kg")
ax.errorbar(x, y, yerr=(yerr, yerr))

plt.show()

We do not currently have any tests of errorbar + units, in interest of getting 3.5.1 out with this fix and the colorbar fixes I move we merge this without a test and kick this particular technical debt can down the road as making sure we have good test coverage is in-scope for the NASA grant.

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Dec 8, 2021
@tacaswell tacaswell added this to the v3.5.1 milestone Dec 8, 2021
Comment thread lib/matplotlib/axes/_axes.py Resolved
jklymak
jklymak previously approved these changes Dec 8, 2021
@QuLogic

QuLogic commented Dec 9, 2021

Copy link
Copy Markdown
Member

cc @neutrinoceros

@neutrinoceros neutrinoceros left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I confirm that this patch

  • fixes the addressed issue
  • fixes unyt's failing test
  • doesn't cause new test failures in unyt

thank you very much for this patch !

@jklymak
jklymak dismissed their stale review December 9, 2021 09:56

Ooops, I guess this is broken somewhere...

@tacaswell

Copy link
Copy Markdown
Member Author

@efiring @dopplershift can you review this (pinging you based on timezone ;) ) so @QuLogic can get 3.5.1 out tonight?

I would also leave this to @QuLogic 's discretion to merge on one (his) review.

@dopplershift dopplershift left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems reasonable. I think it would work for Pint, but honestly I haven't had need for errorbars, so I've never tested.

@efiring efiring left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested clarification of docstring and comments need not delay merging.


If the units are carried on the values then casting to object
arrays preserves the units, but if the units are on the containers
this will not work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find the docstring confusing, mixing the what and the why in a way that I can't sort out.
I suggest simply stating what it does under each case it handles.

"""

# we are here because we the container is not a numpy array, but it
# _is_ iterable (likely a list or a tuple but maybe something more

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But at this point you don't know it is iterable--you check it. If it is not iterable, you still wrap it in an object array.

# you can not directly pass data to the init of `np.ndarray`
if atype is np.ndarray:
return np.asarray(err, dtype=object)
# but you can for unyt and astropy uints

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

astropy "units", I presume

@QuLogic
QuLogic merged commit 5bb1449 into matplotlib:main Dec 10, 2021
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Dec 10, 2021
QuLogic added a commit that referenced this pull request Dec 10, 2021
…884-on-v3.5.x

Backport PR #21884 on branch v3.5.x (FIX: be more careful about coercing unit-full containers to ndarray)
@tacaswell
tacaswell deleted the fix_errorbar_units branch December 10, 2021 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. topic: units and array ducktypes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Matplotlib 3.5 breaks unyt integration of error bars

6 participants