Skip to content

Allow lists of quantities to be converted in Matplotlib unit converter - #7037

Merged
bsipocz merged 3 commits into
astropy:masterfrom
dstansby:list-conversion
Apr 20, 2018
Merged

bsipocz merged 3 commits into
astropy:masterfrom
dstansby:list-conversion

Conversation

@dstansby

Copy link
Copy Markdown
Contributor

Quite a lot of the unit support built into Matplotlib, and quite a lot of the internals, works on lists of unit-like object. This lets a list of quantities be converted fine using the Matplotlib unit converters in Astropy.

As an example, this change makes vlines and hlines work in Matplotlib:

from astropy.visualization import quantity_support
import astropy.units as u
import matplotlib.pyplot as plt

quantity_support()
x = 0.5 * u.m

fig, ax = plt.subplots()
ax.axhline(x)
plt.show()

@astropy-bot

astropy-bot Bot commented Dec 27, 2017 •

Copy link
Copy Markdown

Hi there @dstansby 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog and making sure that this pull request is milestoned and labeled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃.

I noticed the following issue with this pull request:

  • Changelog entry section (v3.1) inconsistent with milestone (v2.0.6)

Would it be possible to fix this? Thanks!

If there are any issues with this message, please report them here.

@mhvk mhvk 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.

Nice simple addition and much more works! Wonderful! I do think I would count this not working as a bug, and thus think we should back-port it. Could you add an entry in CHANGES.rst in the 2.0.4 section?

Also, could you add a simple test to visualization/tests/test_units.py?


with quantity_support():
plt.plot([1, 2, 3] * u.m, [3, 4, 5] * u.kg)
plt.axhline(10 * u.cm)

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.

A horizontal line would have to use the units of the vertical axis, no? So, maybe 10 * u.g?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Woops, that's right. I've tried that, and it throws a different error. I think there's going to need to be a bit more fixing on the Matplotlib side to use axhline actually. I'll let you know when I've had a chance to make it work properley!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, with matplotlib/matplotlib#10143 and this PR axvline and axhline work properly with units. Shall I wait until the Matplotlib one is merged, and then come back to this PR?

@mhvk

mhvk commented Dec 31, 2017

Copy link
Copy Markdown
Contributor

I think I found the reason for the test failures. When you fix it, could you also squash the commits? (I usually do this by rebasing, but if you don't know how to do it, don't worry about it.)

@mhvk

mhvk commented Jan 1, 2018

Copy link
Copy Markdown
Contributor

@dstansby - great that you found where the matplotlib problem is (I'm not sure about the solution, though; it would seem you'd need another transformation -- I don't think you can count on multiplying with units to necessarily work in other unit systems).

For this PR, is there another matplotlib command that is enabled by your change? If so, I think we should add another test that uses that and mark the present test as xfail - with a note that this is a matplotlib bug being fixed. If you don't know about another test, then we probably should wait until the fix in matplotlib is in, and add a version-dependent xfail

@dstansby

dstansby commented Jan 8, 2018

Copy link
Copy Markdown
Contributor Author

🎉 I think that errobar plotting works with this PR - I'll try and put in a test

@mhvk

mhvk commented Jan 9, 2018

Copy link
Copy Markdown
Contributor

Would be wonderful if errorbar worked, but it looks like the X coordinate now gets messed up. Strange.

@dstansby

dstansby commented Jan 9, 2018

Copy link
Copy Markdown
Contributor Author

Bah as far as I can see it's all working locally (maybe because I'm running the master branch of Matplotlib and not a release?), I'll try and have a fresh look at it soon.

@dstansby

Copy link
Copy Markdown
Contributor Author

With this PR, and matplotlib/matplotlib#10206, which has just gone into Matplotlib and will be in version 2.2, axhline works properly, so I'll wait until Matplotlib 2.2 is released to update this PR. (Hopefully then lots of things can be plotted using units!)

@mhvk

mhvk commented Jan 23, 2018

Copy link
Copy Markdown
Contributor

OK, makes sense. It will be nice if units work even more automatically!

@bsipocz bsipocz modified the milestones: v2.0.4, v3.1 Jan 23, 2018
@bsipocz

bsipocz commented Jan 23, 2018

Copy link
Copy Markdown
Member

@dstansby @mhvk - Since this won't be merged any time soon, I'm moving the milestone to 3.1 so we can avoid updating it in every few month. However when this ready to go, please milestone it back to the next 2.0.x milestone, to ensure it gets backported to LTS.

@dstansby

dstansby commented Apr 6, 2018

Copy link
Copy Markdown
Contributor Author

Okay, this change requires Matplotlib version 2.2 or later, but I guess that's better than nothing. In theory it's all good to go now.

@mhvk mhvk 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.

@dstansby - this looks good to me, but I don't understand the many circleci failures. It seems there is something wrong with it that has little to do with your PR. @astrofrog or @bsipocz - do you have a better idea?

Anyway, approved as a PR.

@dstansby

dstansby commented Apr 9, 2018

Copy link
Copy Markdown
Contributor Author

Odd, circleci doesn't even seem to be pulling the branch properly. I'll try closing and re-opening to power cycle CI.

@dstansby dstansby closed this Apr 9, 2018
@dstansby dstansby reopened this Apr 9, 2018
@dstansby

dstansby commented Apr 9, 2018

Copy link
Copy Markdown
Contributor Author

🤦‍♂️ I just force pushed this from an old local copy. Will update it back to the version that was approved when I get to my other computer tomorrow...

@dstansby

Copy link
Copy Markdown
Contributor Author

RIGHT sorry for the faff, this is definitely now ready. pinging @astrofrog @mhvk

@mhvk

mhvk commented Apr 18, 2018

Copy link
Copy Markdown
Contributor

Looks all OK to me. @bsipocz - at some point, you moved the milestone, but is there any reason not to backport to astropy 2.0.x? Matplotlib 2.2 still supports python2.

@bsipocz bsipocz modified the milestones: v3.1, v2.0.6 Apr 18, 2018
@bsipocz

bsipocz commented Apr 18, 2018 •

Copy link
Copy Markdown
Member

It looks it was just a placeholder to avoid unnecessary remilestoning before every bugfix. Moved it back to 2.0.x. (As a consequence, I'm happy to deal with moving the changelog to it's place if you after this is merged).

@bsipocz
bsipocz merged commit 4d9430c into astropy:master Apr 20, 2018
@bsipocz

bsipocz commented Apr 20, 2018

Copy link
Copy Markdown
Member

I'm merging as it got the all OK from @mhvk and I'll fix the milestone/changelog during release as I said above.

Thank you @dstansby!

bsipocz added a commit that referenced this pull request Apr 20, 2018
Allow lists of quantities to be converted in Matplotlib unit converter
bsipocz added a commit that referenced this pull request Apr 20, 2018
Allow lists of quantities to be converted in Matplotlib unit converter
@dstansby
dstansby deleted the list-conversion branch April 20, 2018 22:22
bsipocz added a commit that referenced this pull request Apr 23, 2018
Allow lists of quantities to be converted in Matplotlib unit converter
Cadair added a commit to Cadair/astropy that referenced this pull request May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants