Skip to content

Commit

Permalink
Resolve conflicts with Unidata#2420
Browse files Browse the repository at this point in the history
Reconcile changes in patheffects.py and test_patheffects.py after Unidata#2420.
Also remove Matplotlib<3.3.0 considerations and satisfy the lint robots.
  • Loading branch information
dcamron committed Apr 13, 2023
1 parent 8f603dc commit 2874206
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
25 changes: 9 additions & 16 deletions src/metpy/plots/patheffects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020,2022 MetPy Developers.
# Copyright (c) 2020,2022,2023 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Add effects to matplotlib paths."""
Expand All @@ -9,8 +9,6 @@
import matplotlib.path as mpath
import matplotlib.patheffects as mpatheffects
import matplotlib.transforms as mtransforms
from matplotlib.path import Path
from matplotlib.patheffects import AbstractPathEffect
import numpy as np

from ..package_tools import Exporter
Expand Down Expand Up @@ -138,7 +136,7 @@ def draw_path(self, renderer, gc, path, affine, rgbFace=None): # noqa: N803


@exporter.export
class ScallopedStroke(AbstractPathEffect):
class ScallopedStroke(mpatheffects.AbstractPathEffect):
"""A line-based PathEffect which draws a path with a scalloped style.
The spacing, length, and side of the scallops can be controlled. This implementation is
Expand Down Expand Up @@ -175,19 +173,14 @@ def __init__(self, offset=(0, 0), spacing=10.0, side='left', length=1.15, **kwar
self._length = length
self._gc = kwargs

def draw_path(self, renderer, gc, tpath, affine, rgbFace): # noqa: N803
def draw_path(self, renderer, gc, path, affine, rgbFace=None): # noqa: N803
"""Draw the path with updated gc."""
# Do not modify the input! Use copy instead.
gc0 = renderer.new_gc()
gc0.copy_properties(gc)

gc0 = self._update_gc(gc0, self._gc)
try:
# For matplotlib >= 3.2
trans = affine + self._offset_transform(renderer)
except TypeError:
# For matplotlib < 3.2
trans = self._offset_transform(renderer, affine)
trans = affine + self._offset_transform(renderer)

theta = -np.radians(self._angle)
trans_matrix = np.array([[np.cos(theta), -np.sin(theta)],
Expand All @@ -198,7 +191,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace): # noqa: N803

# Transform before evaluation because to_polygons works at resolution
# of one -- assuming it is working in pixel space.
transpath = affine.transform_path(tpath)
transpath = affine.transform_path(path)

# Evaluate path to straight line segments that can be used to
# construct line scallops.
Expand Down Expand Up @@ -277,12 +270,12 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace): # noqa: N803
i += 2

# Build up vector of Path codes
codes = np.tile([Path.LINETO, Path.CURVE4,
Path.CURVE4, Path.CURVE4], nverts)
codes[0] = Path.MOVETO
codes = np.tile([mpath.Path.LINETO, mpath.Path.CURVE4,
mpath.Path.CURVE4, mpath.Path.CURVE4], nverts)
codes[0] = mpath.Path.MOVETO

# Construct and draw resulting path
h = Path(verts, codes)
h = mpath.Path(verts, codes)

# Transform back to data space during render
renderer.draw_path(gc0, h, affine.inverted() + trans, rgbFace)
Expand Down
40 changes: 18 additions & 22 deletions tests/plots/test_patheffects.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# Copyright (c) 2022 MetPy Developers.
# Copyright (c) 2022,2023 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Test the `patheffects` module."""
import matplotlib.patches as mpatches
import matplotlib.path as mpath
import matplotlib.pyplot as plt
import numpy as np
import pytest
from scipy.interpolate import interp1d

from metpy.plots import ColdFront, OccludedFront, StationaryFront, WarmFront
import matplotlib.patches as patches
from matplotlib.path import Path
from metpy.plots import patheffects

from metpy.plots import ColdFront, OccludedFront, ScallopedStroke, StationaryFront, WarmFront


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.)
Expand Down Expand Up @@ -74,15 +72,14 @@ def test_scalloped_stroke_closed():
y = [1.264, 0.784, -0.076, -0.846, -1.126,
-1.246, -1.006, 0.234, 0.754, 1.264]
verts = np.array([[x, y] for x, y in zip(x, y)])
codes = np.repeat(Path.LINETO, len(x))
codes[0] = Path.MOVETO
codes[-1] = Path.CLOSEPOLY
codes = np.repeat(mpath.Path.LINETO, len(x))
codes[0] = mpath.Path.MOVETO
codes[-1] = mpath.Path.CLOSEPOLY

path = Path(verts, codes)
patch = patches.PathPatch(path, facecolor='#d10000', edgecolor='#000000',
path_effects=[patheffects.ScallopedStroke(side='left',
spacing=10,
length=1.15)])
path = mpath.Path(verts, codes)
patch = mpatches.PathPatch(path, facecolor='#d10000', edgecolor='#000000',
path_effects=[
ScallopedStroke(side='left', spacing=10, length=1.15)])

ax.add_patch(patch)
ax.axis('equal')
Expand All @@ -102,14 +99,13 @@ def test_scalloped_stroke_segment():
x = np.arange(9)
y = np.concatenate([np.arange(5), np.arange(3, -1, -1)])
verts = np.array([[x, y] for x, y in zip(x, y)])
codes = np.repeat(Path.LINETO, len(x))
codes[0] = Path.MOVETO

path = Path(verts, codes)
patch = patches.PathPatch(path, facecolor='none', edgecolor='#000000',
path_effects=[patheffects.ScallopedStroke(side='left',
spacing=10,
length=1.15)])
codes = np.repeat(mpath.Path.LINETO, len(x))
codes[0] = mpath.Path.MOVETO

path = mpath.Path(verts, codes)
patch = mpatches.PathPatch(path, facecolor='none', edgecolor='#000000',
path_effects=[
ScallopedStroke(side='left', spacing=10, length=1.15)])

ax.add_patch(patch)
ax.axis('equal')
Expand Down

0 comments on commit 2874206

Please sign in to comment.