Skip to content

[Bug]: Inconsistent y-axis unit label with plot/scatter #23416

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

Open
dstansby opened this issue Jul 12, 2022 · 14 comments · Fixed by #23417
Open

[Bug]: Inconsistent y-axis unit label with plot/scatter #23416

dstansby opened this issue Jul 12, 2022 · 14 comments · Fixed by #23417

Comments

@dstansby
Copy link
Member

Bug summary

When manually setting the units of an axis using set_units, a plot will show an axis label, but a scatter won't.

The example needs to be run in examples/units to have access to the basic_units.py file.

Code for reproduction

from basic_units import cm
import matplotlib.pyplot as plt
import numpy as np

cms = cm * np.arange(0, 10, 2)

fig, axs = plt.subplots(1, 2, tight_layout=True)

# Does create a y-axis label
axs[0].yaxis.set_units(cm)
axs[0].plot(np.arange(5), cms)

# Does not create a y-axis label
axs[1].yaxis.set_units(cm)
axs[1].scatter(np.arange(5), cms)

plt.show()

Actual outcome

broken

Expected outcome

When the units aren't explicitly set (commenting out set_units()), the output is as expected with both labels:
good

Additional information

No response

Operating system

No response

Matplotlib Version

3.6.0.dev2669+gdc163ca442.d20220712

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

git checkout

@jklymak
Copy link
Member

jklymak commented Jul 12, 2022

I'm not sure which way we should go with this. Pandas and xarray and maybe others autolabel when plot is called. But you almost always have to modify the labels to have actual name of the variable so utility of stating the units seems margins to me. Given that the units don't work properly under change of units I would consider just removing this feature of plot

@dstansby
Copy link
Member Author

Given the current MPL behaviour is to label axes, I think this inconsistency should be fixed to show the label, and changing this behaviour more widely is orthogonal to this bug.

@timhoffm
Copy link
Member

😨 This shows me I understand little of the unit handling.

Feature

I agree with @jklymak that adding a unit label is of questionable value.

Furthermore, the current behavior is quite obscure:

  1. It would be understandable if the label appeared through setting axs[0].yaxis.set_units(cm), but setting that or not does not affect the label.
  2. Instead plotting data with that unit makes it magically appear - unless you've already explicitly set a ylabel before. IMHO this is too clever. "The user has not set a label and plots without labels are bad. Let's take any information we have to make a label."
  3. It only works for Line2D-based data. Nether scatter, bar, stem of fill_between show this behavior.

Consistency

We definitely should make this consistent. Given (3) it's plot() that is the odd ball here. - But granted, weighted by use, it's more important than any single other plot type.

Recommendation

I strongly suggest to deprecate showing the label and get over this magic mess. At our core we want to be a simple understandable library with which you can configure your plot step by step. Note in particular that we do not auto-label plot('x', 'y', data=data) with 'x' and 'y', which would be much more reasonable than this unit stuff.

@dstansby
Copy link
Member Author

I broadly agree with the above comment, but again would say that a long term plan for this labelling is orthogonal to this issue, which is about fixing a specific bug and making the labelling consistent whether set_units() has been called or not.

@timhoffm
Copy link
Member

The thing is that the consistency fix (=adding the label in more cases) is going in the opposite direction of the long-term plan (not having the label at all).

@jklymak
Copy link
Member

jklymak commented Jul 13, 2022

I think the further point is that plot is the outlier here. none of the other plotting methods label axes (that I know of)

@dstansby
Copy link
Member Author

dstansby commented Jul 13, 2022

3. It only works for Line2D-based data. Nether `scatter`, `bar`, `stem` of `fill_between` show this behavior.

I'm pretty sure this is incorrect - I just checked and it works for scatter, bar, stem, and fill_between... so given this I think the original report is a bug to be fixed with the current behaviour, and if we want new behaviour that's a new issue.

Figure_1

from basic_units import cm
import matplotlib.pyplot as plt
import numpy as np

cms = cm * np.arange(0, 10, 2)

fig, axs = plt.subplots(1, 4, tight_layout=True)

axs[0].stem(np.arange(5), cms)
axs[1].bar(np.arange(5), cms)
axs[2].scatter(np.arange(5), cms)
axs[3].fill_between(np.arange(5), cms)
plt.show()

@jklymak
Copy link
Member

jklymak commented Jul 13, 2022

Interesting. OTOH does it work with pcolormesh or contour? (Sorry away from computer)

@dstansby
Copy link
Member Author

Yep, works with both pcolormesh and contour

@jklymak
Copy link
Member

jklymak commented Jul 13, 2022

Well, then I agree it should be consistent... Sorry for not checking...

@timhoffm
Copy link
Member

Sorry for letting this slip. I'll have a look again and respond soonish.

@timhoffm
Copy link
Member

Oh, the issue is not that scatter() etc. does not add the unit, but that axs[1].yaxis.set_units(cm) makes the unit vanish.

@tacaswell
Copy link
Member

This fix ended up breaking pandas so we reverted, re-opening this issues and moving to 3.8

@tacaswell tacaswell modified the milestones: v3.7.0, v3.8.0 Feb 22, 2023
@ksunden
Copy link
Member

ksunden commented Feb 23, 2023

Further discussion is available at #25219 as to how the original fix affected pandas. (just linking here a bit more explicitly)

@ksunden ksunden removed this from the v3.8.0 milestone Sep 15, 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.

6 participants