Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion astropy/tests/figures/py311-test-image-mpl360-cov.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@
"astropy.visualization.wcsaxes.tests.test_transform_coord_meta.TestTransformCoordMeta.test_coords_overlay_auto_coord_meta": "2f737bb70fb1a5452cb0efa79010376614dc559e9aff607f638f044ac6b04448",
"astropy.visualization.wcsaxes.tests.test_transform_coord_meta.TestTransformCoordMeta.test_direct_init": "1f24c5243bfdf0f30e88afc4f2f5d66db85954cea50a2910af94975ff8765b45",
"astropy.visualization.wcsaxes.tests.test_wcsapi.test_wcsapi_5d_with_names": "c90ae6f3b0f9f407ca7a866fa648dc3ef4bea78369315292bc82f16104ed5736",
"astropy.visualization.wcsaxes.tests.test_wcsapi.test_wcsapi_2d_celestial_arcsec": "4b743d645a85d7516decbcf4a831c127af5a1800072597c2a1299d17fb186adb"
"astropy.visualization.wcsaxes.tests.test_wcsapi.test_wcsapi_2d_celestial_arcsec": "4b743d645a85d7516decbcf4a831c127af5a1800072597c2a1299d17fb186adb",
"astropy.visualization.wcsaxes.tests.test_images.test_astropy_support": "aee3dd8cfc0998ec2d904549c6527fa55f3b16cbe9a30c222a2c0ac6a6db155e"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For consistency with existing entries, I would still like this to be moved higher up (where other astropy.visualization.wcsaxes.tests.test_images entries are), ordered alphabetically if possible.

}
3 changes: 2 additions & 1 deletion astropy/tests/figures/py311-test-image-mpldev-cov.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@
"astropy.visualization.wcsaxes.tests.test_transform_coord_meta.TestTransformCoordMeta.test_coords_overlay_auto_coord_meta": "2f737bb70fb1a5452cb0efa79010376614dc559e9aff607f638f044ac6b04448",
"astropy.visualization.wcsaxes.tests.test_transform_coord_meta.TestTransformCoordMeta.test_direct_init": "1f24c5243bfdf0f30e88afc4f2f5d66db85954cea50a2910af94975ff8765b45",
"astropy.visualization.wcsaxes.tests.test_wcsapi.test_wcsapi_5d_with_names": "c90ae6f3b0f9f407ca7a866fa648dc3ef4bea78369315292bc82f16104ed5736",
"astropy.visualization.wcsaxes.tests.test_wcsapi.test_wcsapi_2d_celestial_arcsec": "4b743d645a85d7516decbcf4a831c127af5a1800072597c2a1299d17fb186adb"
"astropy.visualization.wcsaxes.tests.test_wcsapi.test_wcsapi_2d_celestial_arcsec": "4b743d645a85d7516decbcf4a831c127af5a1800072597c2a1299d17fb186adb",
"astropy.visualization.wcsaxes.tests.test_images.test_astropy_support": "aee3dd8cfc0998ec2d904549c6527fa55f3b16cbe9a30c222a2c0ac6a6db155e"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

}
44 changes: 44 additions & 0 deletions astropy/visualization/astropy_support.py
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I cannot find any mention of this new module in https://astropy--17575.org.readthedocs.build/en/17575/visualization/ref_api.html , so none of this docstring is actually being rendered.

You might need to modify https://github.com/astropy/astropy/blob/main/astropy/visualization/__init__.py to fix this. Therefore, defining __all__ in this module is also very important, which I do not see here.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from contextlib import ExitStack, contextmanager

from astropy.visualization.time import time_support
from astropy.visualization.units import quantity_support


@contextmanager
def astropy_support(*, quantity_support_kwargs=None, time_support_kwargs=None):
"""
Enable support for plotting `astropy.units.Quantity` and `astropy.time.Time` instances in
matplotlib.

It can be used as a decorator or with a ``with`` statement.
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.

nit, but I'd like the examples to be presented in the same order they are introduced in this sentence.
(out of scope here, but I'm also realizing that quantity_support and time_support's respective docstrings currently do not demonstrate the decorator style, although they both support it)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, that makes sense. I've made the correction.
Maybe after this PR, another issue can be opened to fix quantity_support and time_support, and have all three in the same module.

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.

I thinkg that moving code around is rarely worth the cost of making git blame less useful. However, +1 on doing a follow up to complete docstrings !


Examples
--------

.. plot::
:include-source:

import matplotlib.pyplot as plt
import astropy.units as u
from astropy.time import Time
from astropy.visualization.astropy_support import astropy_support

@astropy_support()
def plot_example():
plt.figure()
plt.plot([1, 2, 3] * u.m)
plt.plot(Time(['2000-01-01', '2000-01-02', '2000-01-03']).plot_date)
plt.draw()
plt.show()

with astropy_support(): # doctest: +IGNORE_OUTPUT
plt.figure()
plt.plot([1, 2, 3] * u.m)
plt.plot(Time(['2000-01-01', '2000-01-02', '2000-01-03']).plot_date)
plt.draw()

"""
with ExitStack() as stack:
stack.enter_context(quantity_support(**(quantity_support_kwargs or {})))
stack.enter_context(time_support(**(time_support_kwargs or {})))
yield

Check warning on line 44 in astropy/visualization/astropy_support.py

View check run for this annotation

Codecov / codecov/patch

astropy/visualization/astropy_support.py#L41-L44

Added lines #L41 - L44 were not covered by tests
29 changes: 29 additions & 0 deletions astropy/visualization/wcsaxes/tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
)
from astropy.io import fits
from astropy.tests.figures import figure_test
from astropy.time import Time, TimeDelta
from astropy.utils import isiterable
from astropy.utils.data import get_pkg_data_filename
from astropy.utils.exceptions import AstropyUserWarning
from astropy.visualization.astropy_support import astropy_support
from astropy.visualization.wcsaxes import WCSAxes, add_beam, add_scalebar
from astropy.visualization.wcsaxes.frame import EllipticalFrame
from astropy.visualization.wcsaxes.patches import Quadrangle, SphericalCircle
Expand Down Expand Up @@ -1233,6 +1235,33 @@ def test_allsky_labels_wrap():
return fig


@figure_test(tolerance=1)
def test_astropy_support():
x_meters = np.linspace(1, 10, 50) * u.m # Distances in meters
x_kilometers = np.linspace(0.002, 0.011, 50) * u.km # Distances in kilometers

y_seconds = Time("2000-01-01T00:10:00", scale="utc") + TimeDelta(
np.linspace(0, 3600, 50), format="sec"
)

start_time = Time("2000-01-01T00:00:00", scale="utc")
end_time = start_time + TimeDelta(2 / 24, format="jd")

with astropy_support():
fig, ax = plt.subplots()

ax.plot(x_meters, y_seconds, label="Meters vs. Seconds")
ax.plot(x_kilometers, y_seconds, label="Kilometers vs. Seconds")

ax.set_ylim(start_time, Time((end_time), format="jd"))

ax.set_xlabel("Distance (m)")
ax.set_ylabel("Time")
ax.legend()

return fig


@figure_test
def test_tickable_gridlines():
wcs = WCS(
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/visualization/17575.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added a new ``astropy_support`` context manager in ``astropy.visualization``.
This enables ``time_support`` and ``quantity_support`` for plotting ``astropy.units.Quantity`` and ``astropy.time.Time`` instances in matplotlib.