You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
plt.hist() fails with TensorFlow Numpy emulation #19574
Generating np.random.randn(1000) values, visualizing them with plt.hist(). Works fine with Numpy.
When I replace Numpy with tensorflow.experimental.numpy, Matplotlib 3.3.4 fails to display the histogram correctly. Matplotlib 3.2.2 works fine.
Code for reproduction
importmatplotlib.pyplotaspltimportnumpyasnpimporttensorflowastfimporttensorflow.experimental.numpyastnp# bad imagelabels1=15+2*tnp.random.randn(1000)
_=plt.hist(labels1)
# good imagelabels2=15+2*np.random.randn(1000)
_=plt.hist(labels2)
Actual outcome
Expected outcome
Matplotlib version
Operating system: Windows 10
Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.3.4
I think we are happy to help, but we make no guarantee to support all numpy-like objects that exist in the python universe. If you want help with this I suggest figuring out how to reproduce w/o tensorflow so we can advise.
I suggest figuring out how to reproduce w/o tensorflow so we can advise
The Tensorflow failure is the only case I know, and it's how I ended up reporting this bug. I was just trying to learn the Numpy emulation in Tensorflow.
Matplotlib preprocesses instances of np.ndarray so they are always 2D, with vectors gaining an outer dimension. The tensorflow object doesn't get this preprocessing. (Matplotlib should probably be using __array__ here?)
The introspection of the elements in X to test for iterableness (which lets a normal sequence of numbers work as expected) fails because the elements of the tensorflow object are 0D but an instance of Iterable:
It is reasonable to expect that we will ducktype TF arrays correctly here. For now I think there is a user-space work around to cant to numpy before passing to us. That said, none of the core developers are regular TF users so any help with duck-typing them correctly would be greatly appreciated.
Internally we can not directly use np.asarray before handling units because that will break things like pint, but checking for __array__ may work better?
Labeled this as good first issue because I think all of the work that needs to be done is in _reshape_2D and requires little understanding of the rest of Matplotlib internals.
I think it is desirable to ducktype what we can, but we need some rules for what that means. Random object x used to work, but now it doesn't is not a good argument that random x should ever have worked. It would be preferable to try and stipulate what properties x needs to have for successful duck typing.
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
I don't know whether this is still an issue (I think so) but something is a little whacky about this bot: it has been a lot more than 365 days since the last comment here.
I guess if we wanted to be strict, the bot message could say "it has been over 365 days since the last update". It's basically just working through the issue backlog and checking update timestamps.
Activity
jklymak commentedon Feb 25, 2021
I think we are happy to help, but we make no guarantee to support all numpy-like objects that exist in the python universe. If you want help with this I suggest figuring out how to reproduce w/o tensorflow so we can advise.
tacaswell commentedon Feb 25, 2021
This looks like a failure of the up-cast to 2D logic (and instead of 1 histogram of N elements we are seeing N histograms of 1 element).
tacaswell commentedon Feb 25, 2021
Specifically this code:
matplotlib/lib/matplotlib/cbook/__init__.py
Lines 1384 to 1444 in a510e32
which recently got re-worked due to changes in how numpy auto-up-casts object / ragged arrays.
FlorinAndrei commentedon Feb 25, 2021
The Tensorflow failure is the only case I know, and it's how I ended up reporting this bug. I was just trying to learn the Numpy emulation in Tensorflow.
mwaskom commentedon Feb 25, 2021
I think the two problems are:
Matplotlib preprocesses instances of
np.ndarrayso they are always 2D, with vectors gaining an outer dimension. The tensorflow object doesn't get this preprocessing. (Matplotlib should probably be using__array__here?)The introspection of the elements in X to test for iterableness (which lets a normal sequence of numbers work as expected) fails because the elements of the tensorflow object are 0D but an instance of Iterable:
tacaswell commentedon Feb 25, 2021
It is reasonable to expect that we will ducktype TF arrays correctly here. For now I think there is a user-space work around to cant to numpy before passing to us. That said, none of the core developers are regular TF users so any help with duck-typing them correctly would be greatly appreciated.
Internally we can not directly use
np.asarraybefore handling units because that will break things like pint, but checking for__array__may work better?Labeled this as good first issue because I think all of the work that needs to be done is in
_reshape_2Dand requires little understanding of the rest of Matplotlib internals.jklymak commentedon Feb 25, 2021
I think it is desirable to ducktype what we can, but we need some rules for what that means. Random object
xused to work, but now it doesn't is not a good argument that randomxshould ever have worked. It would be preferable to try and stipulate what propertiesxneeds to have for successful duck typing.mwaskom commentedon Feb 25, 2021
Isn't that what
obj.__array__is for?tacaswell commentedon Feb 25, 2021
Long term we can rely on the behavior defined by https://data-apis.org/blog/array_api_standard_release/ but for now I suspect we are going to have to keep playing whack-a-mole with these.
21 remaining items
mwaskom commentedon Aug 21, 2021
Yes but what does it print?
sriakshata commentedon Aug 22, 2021
See above comment https://github.com/matplotlib/matplotlib/issues/19574#issuecomment-902075023. I have attached the output I got.
github-actions commentedon Nov 20, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
mwaskom commentedon Nov 20, 2023
I don't know whether this is still an issue (I think so) but something is a little whacky about this bot: it has been a lot more than 365 days since the last comment here.
rcomer commentedon Nov 20, 2023
I guess if we wanted to be strict, the bot message could say "it has been over 365 days since the last update". It's basically just working through the issue backlog and checking update timestamps.
jklymak commentedon Nov 20, 2023
To clarify further, we set a limit of how many issues get checked and how often so that they don't all get closed at once with no chance for comment.