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

Skip to content

Docs: Units example broken #5051

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
hamogu opened this issue Sep 11, 2015 · 9 comments · Fixed by #25518
Closed

Docs: Units example broken #5051

hamogu opened this issue Sep 11, 2015 · 9 comments · Fixed by #25518

Comments

@hamogu
Copy link
Contributor

hamogu commented Sep 11, 2015

http://matplotlib.org/examples/units/units_scatter.html

shows how to use units and automatic scaling. Looking at the image, plot 2 and 3 look identical, although ax3.yaxis.set_units(minutes) should set the scale of the third plot to minutes. Apparently, the command changes the tick labels, but does not rescale the data.

@WeatherGod
Copy link
Member

Actually, I think the problem is that the third plot has yunits=hertz...
that doesn't seem to make any sense whatever.

On Fri, Sep 11, 2015 at 2:48 PM, Hans Moritz Günther <
[email protected]> wrote:

http://matplotlib.org/examples/units/units_scatter.html

shows how to use units and automatic scaling. Looking at the image, plot 2
and 3 look identical, although ax3.yaxis.set_units(minutes) should set
the scale of the third plot to minutes. Apparently, the command changes the
tick labels, but does not rescale the data.


Reply to this email directly or view it on GitHub
#5051.

@hamogu
Copy link
Contributor Author

hamogu commented Sep 11, 2015

That's what I thought at first. I even started a PR to fix that (but did not upload it). Then I realized that there is the line

ax3.yaxis.set_units(minutes)

So presumably, this ax3 should show the following: We plot with unit Hertz, but then we reset the unit to minutes and the plot changes to adjust that.
Except, that the plot does not change and adjust to it, so it just looks stupid.

@efiring efiring added Documentation Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. topic: units and array ducktypes labels May 23, 2019
@efiring
Copy link
Member

efiring commented May 23, 2019

This is embarrassing. We've had a blatantly nonsensical example sitting around for years. Minimal fix: delete the third subplot. Better fix: figure out how to make the third subplot show something useful, perhaps via fixing the code in basic_units.py.

@efiring
Copy link
Member

efiring commented May 23, 2019

@dopplershift, you are the units expert, right? I know you are not responsible for this example, but maybe you can figure out what to do with it. Or just rip it all out, if it is hopeless.

@dopplershift
Copy link
Contributor

Ooh, a chance to fix a 4-digit issue...

@stevenjlm
Copy link

I could put in a quick fix for this that looks like this example
I looked a little at the underlying problem (that set_units is missing some code to rescale after changing the label) and tried adding some calls to try to get the axis to rescale. I couldn't get it to work. If anyone has idea/suggestions, I could look into it. I'm too new to the codebase to figure this one out on my own though, I think.

@l-johnston
Copy link
Contributor

@dopplershift This example is broken for at least two reasons.

  1. Very early in plotting functions Axes calls cbook._check_1d on the data that then calls np.atleast_1d that strips basic_units.TaggedValue of its units.
  2. scatter doesn't support Axes.set_units because Axes.relim doesn't support Collections.

TaggedValue defines __array__ which is numpy's dispatch mechanism. It is possible to modify it to return an array of TaggedValue objects:

def __array__(self, dtype=object):
        # return np.asarray(self.value).astype(dtype)
        return np.asarray([TaggedValue(v, self.unit) for v in self.value])

This will allow some plotting functions, like plot, to work. But, scatter is still broken due to reason 2. I recommend deleting units_scatter.py.

@jklymak
Copy link
Member

jklymak commented Jul 9, 2021

https://matplotlib.org/stable/gallery/units/bar_unit_demo.html is also broken - bars are provided in centimetres, but then the units are converted to inches, but the numbers don't change.

@tacaswell tacaswell removed the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Aug 18, 2022
@tacaswell tacaswell added this to the needs sorting milestone Aug 18, 2022
@ksunden
Copy link
Member

ksunden commented Mar 20, 2023

The original issue with units_scatter appears to have been fixed insofar as the example being correct by #12422, before much of the conversation here, so I'm slightly confused.

That said, the issue with the bar chart example does appear to persist (and I think as well with changing units on scatter, but not plot, after plotting).

ksunden added a commit to ksunden/matplotlib that referenced this issue Mar 20, 2023
This is admittedly more of a band-aid than a proper fix, but at least
make the generated figure correct.

The underlying problem is the more fundemental one of inconsistent units
behavior in which some artists can be re-united after plotting (e.g.
Line2D), while many others (e.g. Collections) cannot.

Thus, until that is addressed (which is one of the goals of the
data-prototype work), simply setting units early is as good as we can
do.

Closes matplotlib#5051
@QuLogic QuLogic modified the milestones: future releases, v3.7-doc Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants