|
9 | 9 | line segments). |
10 | 10 | """ |
11 | 11 |
|
| 12 | +import inspect |
12 | 13 | import math |
13 | 14 | from numbers import Number |
| 15 | +import warnings |
| 16 | + |
14 | 17 | import numpy as np |
15 | 18 |
|
16 | 19 | import matplotlib as mpl |
17 | 20 | from . import (_api, _path, artist, cbook, cm, colors as mcolors, docstring, |
18 | 21 | hatch as mhatch, lines as mlines, path as mpath, transforms) |
19 | 22 | from ._enums import JoinStyle, CapStyle |
20 | | -import warnings |
21 | 23 |
|
22 | 24 |
|
23 | 25 | # "color" is excluded; it is a compound setter, and its docstring differs |
@@ -1991,20 +1993,59 @@ class QuadMesh(Collection): |
1991 | 1993 |
|
1992 | 1994 | *shading* may be 'flat', or 'gouraud' |
1993 | 1995 | """ |
1994 | | - def __init__(self, meshWidth, meshHeight, coordinates, |
1995 | | - antialiased=True, shading='flat', **kwargs): |
| 1996 | + def __init__(self, *args, **kwargs): |
| 1997 | + # signature deprecation since="3.5": Change to new signature after the |
| 1998 | + # deprecation has expired. Also remove setting __init__.__signature__, |
| 1999 | + # and remove the Notes from the docstring. |
| 2000 | + # |
| 2001 | + # We use lambdas to parse *args, **kwargs through the respective old |
| 2002 | + # and new signatures. |
| 2003 | + try: |
| 2004 | + # Old signature: |
| 2005 | + # The following raises a TypeError iif the args don't match. |
| 2006 | + w, h, coords, antialiased, shading, kwargs = ( |
| 2007 | + lambda meshWidth, meshHeight, coordinates, antialiased=True, |
| 2008 | + shading=False, **kwargs: |
| 2009 | + (meshWidth, meshHeight, coordinates, antialiased, shading, |
| 2010 | + kwargs))(*args, **kwargs) |
| 2011 | + except TypeError as exc: |
| 2012 | + # New signature: |
| 2013 | + # If the following raises a TypeError (i.e. args don't match), |
| 2014 | + # just let it propagate. |
| 2015 | + coords, antialiased, shading, kwargs = ( |
| 2016 | + lambda coordinates, antialiased=True, shading=False, **kwargs: |
| 2017 | + (coordinates, antialiased, shading, kwargs))(*args, **kwargs) |
| 2018 | + coords = np.asarray(coords, np.float64) |
| 2019 | + else: # The old signature matched. |
| 2020 | + _api.warn_deprecated( |
| 2021 | + "3.5", |
| 2022 | + message="This usage of Quadmesh is deprecated: Parameters " |
| 2023 | + "meshWidth and meshHights will be removed; " |
| 2024 | + "coordinates must be 2D; all parameters except " |
| 2025 | + "coordinates will be keyword-only.") |
| 2026 | + coords = np.asarray(coords, np.float64).reshape((h + 1, w + 1, 2)) |
| 2027 | + # end of signature deprecation code |
| 2028 | + |
1996 | 2029 | super().__init__(**kwargs) |
1997 | | - self._meshWidth = meshWidth |
1998 | | - self._meshHeight = meshHeight |
1999 | | - # By converting to floats now, we can avoid that on every draw. |
2000 | | - self._coordinates = np.asarray(coordinates, float).reshape( |
2001 | | - (meshHeight + 1, meshWidth + 1, 2)) |
| 2030 | + self._coordinates = coords |
| 2031 | + shape = self._coordinates.shape |
| 2032 | + if (self._coordinates.ndim != 3 or shape[-1] != 2): |
| 2033 | + raise ValueError( |
| 2034 | + "coordinates must be a (N, M, 2) array-like, but got " |
| 2035 | + f"{shape}") |
| 2036 | + |
| 2037 | + self._meshWidth = shape[1] - 1 |
| 2038 | + self._meshHeight = shape[0] - 1 |
2002 | 2039 | self._antialiased = antialiased |
2003 | 2040 | self._shading = shading |
2004 | 2041 |
|
2005 | 2042 | self._bbox = transforms.Bbox.unit() |
2006 | | - self._bbox.update_from_data_xy(coordinates.reshape( |
2007 | | - ((meshWidth + 1) * (meshHeight + 1), 2))) |
| 2043 | + self._bbox.update_from_data_xy(self._coordinates.reshape(-1, 2)) |
| 2044 | + |
| 2045 | + # Only needed during signature deprecation |
| 2046 | + __init__.__signature__ = inspect.signature( |
| 2047 | + lambda self, coordinates, *, |
| 2048 | + antialiased=True, shading='flat', **kwargs: None) |
2008 | 2049 |
|
2009 | 2050 | def get_paths(self): |
2010 | 2051 | if self._paths is None: |
|
0 commit comments