8000 Merge pull request #24356 from oscargus/quadmeshsignatureremoval · matplotlib/matplotlib@61c67e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61c67e4

Browse files
authored
Merge pull request #24356 from oscargus/quadmeshsignatureremoval
Expire QuadMesh old signature deprecation
2 parents 67bcb90 + 091c7ce commit 61c67e4

File tree

3 files changed

+31
-137
lines changed

3 files changed

+31
-137
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
``QuadMesh`` signature
2+
~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The `.QuadMesh` signature ::
5+
6+
def __init__(meshWidth, meshHeight, coordinates,
7+
antialiased=True, shading='flat', **kwargs)
8+
9+
is removed and replaced by the new signature ::
10+
11+
def __init__(coordinates, *, antialiased=True, shading='flat', **kwargs)
12+
13+
In particular:
14+
15+
- The *coordinates* argument must now be a (M, N, 2) array-like. Previously,
16+
the grid shape was separately specified as (*meshHeight* + 1, *meshWidth* +
17+
1) and *coordinates* could be an array-like of any shape with M * N * 2
18+
elements.
19+
- All parameters except *coordinates* are keyword-only now.

lib/matplotlib/collections.py

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
line segments).
1010
"""
1111

12-
import inspect
1312
import math
1413
from numbers import Number
1514
import warnings
@@ -1923,62 +1922,15 @@ class QuadMesh(Collection):
19231922
i.e. `~.Artist.contains` checks whether the test point is within any of the
19241923
mesh quadrilaterals.
19251924
1926-
There exists a deprecated API version ``QuadMesh(M, N, coords)``, where
1927-
the dimensions are given explicitly and ``coords`` is a (M*N, 2)
1928-
array-like. This has been deprecated in Matplotlib 3.5. The following
1929-
describes the semantics of this deprecated API.
1930-
1931-
A quadrilateral mesh consists of a grid of vertices.
1932-
The dimensions of this array are (*meshWidth* + 1, *meshHeight* + 1).
1933-
Each vertex in the mesh has a different set of "mesh coordinates"
1934-
representing its position in the topology of the mesh.
1935-
For any values (*m*, *n*) such that 0 <= *m* <= *meshWidth*
1936-
and 0 <= *n* <= *meshHeight*, the vertices at mesh coordinates
1937-
(*m*, *n*), (*m*, *n* + 1), (*m* + 1, *n* + 1), and (*m* + 1, *n*)
1938-
form one of the quadrilaterals in the mesh. There are thus
1939-
(*meshWidth* * *meshHeight*) quadrilaterals in the mesh. The mesh
1940-
need not be regular and the polygons need not be convex.
1941-
1942-
A quadrilateral mesh is represented by a (2 x ((*meshWidth* + 1) *
1943-
(*meshHeight* + 1))) numpy array *coordinates*, where each row is
1944-
the *x* and *y* coordinates of one of the vertices. To define the
1945-
function that maps from a data point to its corresponding color,
1946-
use the :meth:`set_cmap` method. Each of these arrays is indexed in
1947-
row-major order by the mesh coordinates of the vertex (or the mesh
1948-
coordinates of the lower left vertex, in the case of the colors).
1949-
1950-
For example, the first entry in *coordinates* is the coordinates of the
1951-
vertex at mesh coordinates (0, 0), then the one at (0, 1), then at (0, 2)
1952-
.. (0, meshWidth), (1, 0), (1, 1), and so on.
19531925
"""
19541926

1955-
def __init__(self, *args, **kwargs):
1956-
# signature deprecation since="3.5": Change to new signature after the
1957-
# deprecation has expired. Also remove setting __init__.__signature__,
1958-
# and remove the Notes from the docstring.
1959-
params = _api.select_matching_signature(
1960-
[
1961-
lambda meshWidth, meshHeight, coordinates, antialiased=True,
1962-
shading='flat', **kwargs: locals(),
1963-
lambda coordinates, antialiased=True, shading='flat', **kwargs:
1964-
locals()
1965-
],
1966-
*args, **kwargs).values()
1967-
*old_w_h, coords, antialiased, shading, kwargs = params
1968-
if old_w_h: # The old signature matched.
1969-
_api.warn_deprecated(
1970-
"3.5",
1971-
message="This usage of Quadmesh is deprecated: Parameters "
1972-
"meshWidth and meshHeights will be removed; "
1973-
"coordinates must be 2D; all parameters except "
1974-
"coordinates will be keyword-only.")
1975-
w, h = old_w_h
1976-
coords = np.asarray(coords, np.float64).reshape((h + 1, w + 1, 2))
1927+
def __init__(self, coordinates, *, antialiased=True, shading='flat',
1928+
**kwargs):
19771929
kwargs.setdefault("pickradius", 0)
19781930
# end of signature deprecation code
19791931

1980-
_api.check_shape((None, None, 2), coordinates=coords)
1981-
self._coordinates = coords
1932+
_api.check_shape((None, None, 2), coordinates=coordinates)
1933+
self._coordinates = coordinates
19821934
self._antialiased = antialiased
19831935
self._shading = shading
19841936
self._bbox = transforms.Bbox.unit()
@@ -1988,11 +1940,6 @@ def __init__(self, *args, **kwargs):
19881940
super().__init__(**kwargs)
19891941
self.set_mouseover(False)
19901942

1991-
# Only needed during signature deprecation
1992-
__init__.__signature__ = inspect.signature(
1993-
lambda self, coordinates, *,
1994-
antialiased=True, shading='flat', pickradius=0, **kwargs: None)
1995-
19961943
def get_paths(self):
19971944
if self._paths is None:
19981945
self.set_paths()
@@ -2036,11 +1983,10 @@ def set_array(self, A):
20361983
faulty_data = True
20371984

20381985
if misshapen_data:
2039-
_api.warn_deprecated(
2040-
"3.5", message=f"For X ({width}) and Y ({height}) "
2041-
f"with {self._shading} shading, the expected shape of "
2042-
f"A is ({h}, {w}). Passing A ({A.shape}) is deprecated "
2043-
"since %(since)s and will become an error %(removal)s.")
1986+
raise ValueError(
1987+
f"For X ({width}) and Y ({height}) with {self._shading} "
1988+
f"shading, the expected shape of A is ({h}, {w}), not "
1989+
f"{A.shape}")
20441990

20451991
if faulty_data:
20461992
raise TypeError(

lib/matplotlib/tests/test_collections.py

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import matplotlib.path as mpath
1313
import matplotlib.transforms as mtransforms
1414
from matplotlib.collections import (Collection, LineCollection,
15-
EventCollection, PolyCollection,
16-
QuadMesh)
15+
EventCollection, PolyCollection)
1716
from matplotlib.testing.decorators import check_figures_equal, image_comparison
18-
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
1917

2018

2119
def generate_EventCollection_plot():
@@ -811,77 +809,6 @@ def test_autolim_with_zeros(transform, expected):
811809
np.testing.assert_allclose(ax.get_xlim(), expected)
812810

813811

814-
@pytest.mark.parametrize('flat_ref, kwargs', [
815-
(True, {}),
816-
(False, {}),
817-
(True, dict(antialiased=False)),
818-
(False, dict(transform='__initialization_delayed__')),
819-
])
820-
@check_figures_equal(extensions=['png'])
821-
def test_quadmesh_deprecated_signature(
822-
fig_test, fig_ref, flat_ref, kwargs):
823-
# test that the new and old quadmesh signature produce the same results
824-
# remove when the old QuadMesh.__init__ signature expires (v3.5+2)
825-
x = [0, 1, 2, 3.]
826-
y = [1, 2, 3.]
827-
X, Y = np.meshgrid(x, y)
828-
X += 0.2 * Y
829-
coords = np.stack([X, Y], axis=-1)
830-
assert coords.shape == (3, 4, 2)
831-
C = np.linspace(0, 2, 6).reshape(2, 3)
832-
833-
ax = fig_test.add_subplot()
834-
ax.set(xlim=(0, 5), ylim=(0, 4))
835-
if 'transform' in kwargs:
836-
kwargs['transform'] = mtransforms.Affine2D().scale(1.2) + ax.transData
837-
qmesh = QuadMesh(coords, **kwargs)
838-
qmesh.set_array(C)
839-
ax.add_collection(qmesh)
840-
assert qmesh._shading == 'flat'
841-
842-
ax = fig_ref.add_subplot()
843-
ax.set(xlim=(0, 5), ylim=(0, 4))
844-
if 'transform' in kwargs:
845-
kwargs['transform'] = mtransforms.Affine2D().scale(1.2) + ax.transData
846-
with pytest.warns(MatplotlibDeprecationWarning):
847-
qmesh = QuadMesh(4 - 1, 3 - 1,
848-
coords.copy().reshape(-1, 2) if flat_ref else coords,
849-
**kwargs)
850-
qmesh.set_array(C.flatten() if flat_ref else C)
851-
ax.add_collection(qmesh)
852-
assert qmesh._shading == 'flat'
853-
854-
855-
@check_figures_equal(extensions=['png'])
856-
def test_quadmesh_deprecated_positional(fig_test, fig_ref):
857-
# test that positional parameters are still accepted with the old signature
858-
# and work correctly
859-
# remove when the old QuadMesh.__init__ signature expires (v3.5+2)
860-
from matplotlib.collections import QuadMesh
861-
862-
x = [0, 1, 2, 3.]
863-
y = [1, 2, 3.]
864-
X, Y = np.meshgrid(x, y)
865-
X += 0.2 * Y
866-
coords = np.stack([X, Y], axis=-1)
867-
assert coords.shape == (3, 4, 2)
868-
C = np.linspace(0, 2, 12).reshape(3, 4)
869-
870-
ax = fig_test.add_subplot()
871-
ax.set(xlim=(0, 5), ylim=(0, 4))
872-
qmesh = QuadMesh(coords, antialiased=False, shading='gouraud')
873-
qmesh.set_array(C)
874-
ax.add_collection(qmesh)
875-
876-
ax = fig_ref.add_subplot()
877-
ax.set(xlim=(0, 5), ylim=(0, 4))
878-
with pytest.warns(MatplotlibDeprecationWarning):
879-
qmesh = QuadMesh(4 - 1, 3 - 1, coords.copy().reshape(-1, 2),
880-
False, 'gouraud')
881-
qmesh.set_array(C)
882-
ax.add_collection(qmesh)
883-
884-
885812
def test_quadmesh_set_array_validation():
886813
x = np.arange(11)
887814
y = np.arange(8)
@@ -890,7 +817,9 @@ def test_quadmesh_set_array_validation():
890817
coll = ax.pcolormesh(x, y, z)
891818

892819
# Test deprecated warning when faulty shape is passed.
893-
with pytest.warns(MatplotlibDeprecationWarning):
820+
with pytest.raises(ValueError, match=r"For X \(11\) and Y \(8\) with flat "
821+
r"shading, the expected shape of A is \(7, 10\), not "
822+
r"\(10, 7\)"):
894823
coll.set_array(z.reshape(10, 7))
895824

896825
z = np.arange(54).reshape((6, 9))

0 commit comments

Comments
 (0)
0