diff --git a/astropy/visualization/wcsaxes/coordinate_helpers.py b/astropy/visualization/wcsaxes/coordinate_helpers.py index 8bcb1b2a555..a1064057644 100644 --- a/astropy/visualization/wcsaxes/coordinate_helpers.py +++ b/astropy/visualization/wcsaxes/coordinate_helpers.py @@ -23,10 +23,10 @@ from .grid_paths import get_gridline_path, get_lon_lat_path from .ticklabels import TickLabels from .ticks import Ticks +from .utils import MATPLOTLIB_LT_3_8 __all__ = ["CoordinateHelper"] - # Matplotlib's gridlines use Line2D, but ours use PathPatch. # Patches take a slightly different format of linestyle argument. LINES_TO_PATCHES_LINESTYLE = { @@ -618,9 +618,13 @@ def _draw_grid(self, renderer): p.draw(renderer) elif self._grid is not None: - for line in self._grid.collections: - line.set(**self.grid_lines_kwargs) - line.draw(renderer) + if MATPLOTLIB_LT_3_8: + for line in self._grid.collections: + line.set(**self.grid_lines_kwargs) + line.draw(renderer) + else: + self._grid.set(**self.grid_lines_kwargs) + self._grid.draw(renderer) renderer.close_group("grid lines") @@ -1123,8 +1127,11 @@ def _get_gridline(self, xy_world, pixel, xy_world_round): def _clear_grid_contour(self): if hasattr(self, "_grid") and self._grid: - for line in self._grid.collections: - line.remove() + if MATPLOTLIB_LT_3_8: + for line in self._grid.collections: + line.remove() + else: + self._grid.remove() def _update_grid_contour(self): if self.coord_index is None: diff --git a/astropy/visualization/wcsaxes/utils.py b/astropy/visualization/wcsaxes/utils.py index 83149d34fce..3b040ef7485 100644 --- a/astropy/visualization/wcsaxes/utils.py +++ b/astropy/visualization/wcsaxes/utils.py @@ -1,10 +1,11 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst - +import matplotlib import numpy as np from astropy import units as u from astropy.coordinates import BaseCoordinateFrame, UnitSphericalRepresentation +from astropy.utils.introspection import minversion __all__ = [ "select_step_degree", @@ -13,6 +14,8 @@ "transform_contour_set_inplace", ] +MATPLOTLIB_LT_3_8 = not minversion(matplotlib, "3.8.dev") + def select_step_degree(dv): # Modified from axis_artist, supports astropy.units @@ -136,17 +139,28 @@ def transform_contour_set_inplace(cset, transform): pos_level = [] pos_segments = [] - for collection in cset.collections: - paths = collection.get_paths() - if len(paths) == 0: - continue - all_paths.append(paths) - # The last item in pos isn't needed for np.split and in fact causes - # issues if we keep it because it will cause an extra empty array to be - # returned. - pos = np.cumsum([len(x) for x in paths]) - pos_segments.append(pos[:-1]) - pos_level.append(pos[-1]) + if MATPLOTLIB_LT_3_8: + for collection in cset.collections: + paths = collection.get_paths() + if len(paths) == 0: + continue + all_paths.append(paths) + # The last item in pos isn't needed for np.split and in fact causes + # issues if we keep it because it will cause an extra empty array to be + # returned. + pos = np.cumsum([len(x) for x in paths]) + pos_segments.append(pos[:-1]) + pos_level.append(pos[-1]) + else: + paths = cset.get_paths() + if len(paths) > 0: + all_paths.append(paths) + # The last item in pos isn't needed for np.split and in fact causes + # issues if we keep it because it will cause an extra empty array to be + # returned. + pos = np.cumsum([len(x) for x in paths]) + pos_segments.append(pos[:-1]) + pos_level.append(pos[-1]) # As above the last item isn't needed pos_level = np.cumsum(pos_level)[:-1]