Skip to content

Commit

Permalink
Merge pull request astropy#15011 from meeseeksmachine/auto-backport-o…
Browse files Browse the repository at this point in the history
…f-pr-15004-on-v5.3.x

Backport PR astropy#15004 on branch v5.3.x (MNT: Compatibility with matplotlib 3.8)
  • Loading branch information
pllim committed Jul 2, 2023
2 parents ba6bf56 + 5675039 commit caa4d7c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
19 changes: 13 additions & 6 deletions astropy/visualization/wcsaxes/coordinate_helpers.py
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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:
Expand Down
38 changes: 26 additions & 12 deletions 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",
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit caa4d7c

Please sign in to comment.