10000 updated class hierarchy for Colorizer · matplotlib/matplotlib@f2a0ba1 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2a0ba1

Browse files
committed
updated class hierarchy for Colorizer
1 parent d130006 commit f2a0ba1

File tree

5 files changed

+80
-75
lines changed

5 files changed

+80
-75
lines changed

lib/matplotlib/artist.py

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import numpy as np
1313

1414
import matplotlib as mpl
15-
from . import _api, cbook
15+
from . import _api, cbook, colorizer
1616
from .colors import BoundaryNorm
17-
from .cm import ScalarMappable
18-
from .colorizer import Colorizer
1917
from .path import Path
2018
from .transforms import (BboxBase, Bbox, IdentityTransform, Transform, TransformedBbox,
2119
TransformedPatchPath, TransformedPath)
@@ -1347,8 +1345,8 @@ def format_cursor_data(self, data):
13471345
--------
13481346
get_cursor_data
13491347
"""
1350-
if np.ndim(data) == 0 and (isinstance(self, ScalarMappable) or
1351-
isinstance(self, ColorizingArtist)):
1348+
if np.ndim(data) == 0 and (isinstance(self, mpl.cm.ScalarMappable) or
1349+
isinstance(self, colorizer.ColorizingArtist)):
13521350
# This block logically belongs to ScalarMappable, but can't be
13531351
# implemented in it because most ScalarMappable subclasses inherit
13541352
# from Artist first and from ScalarMappable second, so
@@ -1419,67 +1417,6 @@ def set_mouseover(self, mouseover):
14191417
mouseover = property(get_mouseover, set_mouseover) # backcompat.
14201418

14211419

1422-
class ColorizingArtist(Artist):
1423-
def __init__(self, colorizer):
1424-
"""
1425-
Parameters
1426-
----------
1427-
colorizer : `colorizer.Colorizer`
1428-
"""
1429-
if not isinstance(colorizer, Colorizer):
1430-
raise ValueError("A `mpl.colorizer.Colorizer` object must be provided")
1431-
1432-
Artist.__init__(self)
1433-
1434-
self._A = None
1435-
1436-
self.colorizer = colorizer
1437-
self._id_colorizer = self.colorizer.callbacks.connect('changed', self.changed)
1438-
self.callbacks = cbook.CallbackRegistry(signals=["changed"])
1439-
1440-
def set_array(self, A):
1441-
"""
1442-
Set the value array from array-like *A*.
1443-
1444-
Parameters
1445-
----------
1446-
A : array-like or None
1447-
The values that are mapped to colors.
1448-
1449-
The base class `.ColorizingArtist` does not make any assumptions on
1450-
the dimensionality and shape of the value array *A*.
1451-
"""
1452-
if A is None:
1453-
self._A = None
1454-
return
1455-
1456-
A = cbook.safe_masked_invalid(A, copy=True)
1457-
if not np.can_cast(A.dtype, float, "same_kind"):
1458-
raise TypeError(f"Image data of dtype {A.dtype} cannot be "
1459-
"converted to float")
1460-
1461-
self._A = A
1462-
if not self.norm.scaled():
1463-
self.colorizer.autoscale_None(A)
1464-
1465-
def get_array(self):
1466-
"""
1467-
Return the array of values, that are mapped to colors.
1468-
1469-
The base class `.ColorizingArtist` does not make any assumptions on
1470-
the dimensionality and shape of the array.
1471-
"""
1472-
return self._A
1473-
1474-
def changed(self):
1475-
"""
1476-
Call this whenever the mappable is changed to notify all the
1477-
callbackSM listeners to the 'changed' signal.
1478-
"""
1479-
self.callbacks.process('changed')
1480-
self.stale = True
1481-
1482-
14831420
def _get_tightbbox_for_layout_only(obj, *args, **kwargs):
14841421
"""
14851422
Matplotlib's `.Axes.get_tightbbox` and `.Axis.get_tightbbox` support a

lib/matplotlib/cm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def get_cmap(name=None, lut=None):
275275
return _colormaps[name].resampled(lut)
276276

277277

278-
class ScalarMappable(colorizer.ColorizerShim):
278+
class ScalarMappable(colorizer._ColorizerInterface):
279279
"""
280280
A mixin class to map one or multiple sets of scalar data to RGBA.
281281

lib/matplotlib/collections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"linewidth": ["linewidths", "lw"],
3333
"offset_transform": ["transOffset"],
3434
})
35-
class Collection(artist.ColorizingArtist, colorizer.ColorizerShim):
35+
class Collection(colorizer.ColorizingArtist):
3636
r"""
3737
Base class for Collections. Must be subclassed to be usable.
3838
@@ -156,7 +156,7 @@ def __init__(self, *,
156156
``Collection.set_{key}(val)`` for each key-value pair in *kwargs*.
157157
"""
158158

159-
artist.ColorizingArtist.__init__(self, colorizer._get_colorizer(cmap, norm))
159+
colorizer.ColorizingArtist.__init__(self, colorizer._get_colorizer(cmap, norm))
160160
# list of un-scaled dash patterns
161161
# this is needed scaling the dash pattern by linewidth
162162
self._us_linestyles = [(0, None)]

lib/matplotlib/colorizer.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import numpy as np
1919
from numpy import ma
2020
import functools
21-
from matplotlib import _api, colors, cbook, scale, cm
21+
from matplotlib import _api, colors, cbook, scale, cm, artist
2222

2323

2424
class Colorizer():
@@ -318,8 +318,15 @@ def _get_colorizer(cmap, norm):
318318
return Colorizer(cmap, norm)
319319

320320

321-
class ColorizerShim:
321+
class _ColorizerInterface:
322+
"""
323+
Base class that contains the interface to `Colorizer` objects from
324+
a `ColorizingArtist` or `cm.ScalarMappable`.
322325
326+
Note: This class only contain functions that interface the .colorizer
327+
attribute. Other functions that as shared between `ColorizingArtist`
328+
and `cm.ScalarMappable` are not included.
329+
"""
323330
def _scale_norm(self, norm, vmin, vmax):
324331
self.colorizer._scale_norm(norm, vmin, vmax, self._A)
325332

@@ -458,6 +465,67 @@ def colorbar(self, colorbar):
458465
self.colorizer.colorbar = colorbar
459466

460467

468+
class ColorizingArtist(artist.Artist, _ColorizerInterface):
469+
def __init__(self, colorizer):
470+
"""
471+
Parameters
472+
----------
473+
colorizer : `colorizer.Colorizer`
474+
"""
475+
if not isinstance(colorizer, Colorizer):
476+
raise ValueError("A `mpl.colorizer.Colorizer` object must be provided")
477+
478+
artist.Artist.__init__(self)
479+
480+
self._A = None
481+
482+
self.colorizer = colorizer
483+
self._id_colorizer = self.colorizer.callbacks.connect('changed', self.changed)
484+
self.callbacks = cbook.CallbackRegistry(signals=["changed"])
485+
486+
def set_array(self, A):
487+
"""
488+
Set the value array from array-like *A*.
489+
490+
Parameters
491+
----------
492+
A : array-like or None
493+
The values that are mapped to colors.
494+
495+
The base class `.ColorizingArtist` does not make any assumptions on
496+
the dimensionality and shape of the value array *A*.
497+
"""
498+
if A is None:
499+
self._A = None
500+
return
501+
502+
A = cbook.safe_masked_invalid(A, copy=True)
503+
if not np.can_cast(A.dtype, float, "same_kind"):
504+
raise TypeError(f"Image data of dtype {A.dtype} cannot be "
505+
"converted to float")
506+
507+
self._A = A
508+
if not self.norm.scaled():
509+
self.colorizer.autoscale_None(A)
510+
511+
def get_array(self):
512+
"""
513+
Return the array of values, that are mapped to colors.
514+
515+
The base class `.ColorizingArtist` does not make any assumptions on
516+
the dimensionality and shape of the array.
517+
"""
518+
return self._A
519+
520+
def changed(self):
521+
"""
522+
Call this whenever the mappable is changed to notify all the
523+
callbackSM listeners to the 'changed' signal.
524+
"""
525+
self.callbacks.process('changed')
526+
self.stale = True
527+
528+
461529
def _auto_norm_from_scale(scale_cls):
462530
"""
463531
Automatically generate a norm class from *scale_cls*.

lib/matplotlib/image.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def _rgb_to_rgba(A):
229229
return rgba
230230

231231

232-
class _ImageBase(martist.ColorizingArtist, colorizer.ColorizerShim):
232+
class _ImageBase(colorizer.ColorizingArtist):
233233
"""
234234
Base class for images.
235235
@@ -258,7 +258,7 @@ def __init__(self, ax,
258258
interpolation_stage=None,
259259
**kwargs
260260
):
261-
martist.ColorizingArtist.__init__(self, colorizer._get_colorizer(cmap, norm))
261+
colorizer.ColorizingArtist.__init__(self, colorizer._get_colorizer(cmap, norm))
262262
if origin is None:
263263
origin = mpl.rcParams['image.origin']
264264
_api.check_in_list(["upper", "lower"], origin=origin)
@@ -330,7 +330,7 @@ def changed(self):
330330
Call this whenever the mappable is changed so observers can update.
331331
"""
332332
self._imcache = None
333-
martist.ColorizingArtist.changed(self)
333+
colorizer.ColorizingArtist.changed(self)
334334

335335
def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
336336
unsampled=False, round_to_pixel_border=True):
@@ -1350,7 +1350,7 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
13501350

13511351
def set_data(self, A):
13521352
"""Set the image array."""
1353-
martist.ColorizingArtist.set_array(self, A)
1353+
colorizer.ColorizingArtist.set_array(self, A)
13541354
self.stale = True
13551355

13561356

0 commit comments

Comments
 (0)
0