Skip to content

Commit 4d9430c

Browse files
authored
Merge pull request astropy#7037 from dstansby/list-conversion
Allow lists of quantities to be converted in Matplotlib unit converter
2 parents e96db68 + 75a8360 commit 4d9430c

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎CHANGES.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ astropy.utils
8686

8787
astropy.visualization
8888
^^^^^^^^^^^^^^^^^^^^^
89+
- Lists of units are now converted in the Matplotlib unit converter. This means
90+
that for Matplotlib versions later than 2.2, more plotting functions now work
91+
with units (e.g. errorbar). [#7037]
8992

9093
astropy.wcs
9194
^^^^^^^^^^^

‎astropy/visualization/tests/test_units.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ def test_units():
3737
plt.clf()
3838

3939

40+
@pytest.mark.skipif('not HAS_PLT')
41+
def test_units_errbarr():
42+
pytest.importorskip("matplotlib", minversion="2.2")
43+
plt.figure()
44+
45+
with quantity_support():
46+
x = [1, 2, 3] * u.s
47+
y = [1, 2, 3] * u.m
48+
yerr = [3, 2, 1] * u.cm
49+
50+
fig, ax = plt.subplots()
51+
ax.errorbar(x, y, yerr=yerr)
52+
53+
assert ax.xaxis.get_units() == u.s
54+
assert ax.yaxis.get_units() == u.m
55+
56+
plt.clf()
57+
58+
4059
@pytest.mark.skipif('not HAS_PLT')
4160
def test_incompatible_units():
4261
plt.figure()

‎astropy/visualization/units.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def axisinfo(unit, axis):
8080
def convert(val, unit, axis):
8181
if isinstance(val, u.Quantity):
8282
return val.to_value(unit)
83+
elif isinstance(val, list):
84+
return [v.to_value(unit) for v in val]
8385
else:
8486
return val
8587

0 commit comments

Comments
 (0)