Skip to content

Commit e170f99

Browse files
committed
update units.ConversionInterface to have to_numeric and from_numeric
This change updates `matplotlib.units.ConversionInterface` to have a `to_numeric` instead of a `convert` method, and adds a `from_numeric` method. The method `convert` is renamed to `to_numeric` and similarly named methods are updated accordingly. The renaming is done to make `convert` less ambiguous with a forward and backward conversion on the `ConversionInterface`. Adding a `from_numeric` method standardizes the way to convert back to a native object from an internal numeric value. This allows registered converters to provide more specific conversions which will allow formatters and locators to provide an implementation which is not as tied to the internal values Matplotlib uses.
1 parent d4e834f commit e170f99

29 files changed

+441
-291
lines changed

doc/api/artist_api.rst

+2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ Units
144144
:toctree: _as_gen
145145
:nosignatures:
146146

147+
Artist.convert_x_to_numeric
147148
Artist.convert_xunits
149+
Artist.convert_y_to_numeric
148150
Artist.convert_yunits
149151
Artist.have_units
150152

doc/api/axes_api.rst

+2
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ Units
424424
:template: autosummary.rst
425425
:nosignatures:
426426

427+
Axes.convert_x_to_numeric
427428
Axes.convert_xunits
429+
Axes.convert_y_to_numeric
428430
Axes.convert_yunits
429431
Axes.have_units
430432

doc/api/axis_api.rst

+18
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ Units
159159
:template: autosummary.rst
160160
:nosignatures:
161161

162+
Axis.convert_from_numeric
163+
Axis.convert_to_numeric
162164
Axis.convert_units
163165
Axis.set_units
164166
Axis.get_units
@@ -330,6 +332,8 @@ XAxis
330332
XAxis.OFFSETTEXTPAD
331333
XAxis.axis_date
332334
XAxis.cla
335+
XAxis.convert_from_numeric
336+
XAxis.convert_to_numeric
333337
XAxis.convert_units
334338
XAxis.get_data_interval
335339
XAxis.get_gridlines
@@ -395,6 +399,8 @@ YAxis
395399
YAxis.OFFSETTEXTPAD
396400
YAxis.axis_date
397401
YAxis.cla
402+
YAxis.convert_from_numeric
403+
YAxis.convert_to_numeric
398404
YAxis.convert_units
399405
YAxis.get_data_interval
400406
YAxis.get_gridlines
@@ -464,7 +470,9 @@ Ticks
464470
Tick.add_callback
465471
Tick.axes
466472
Tick.contains
473+
Tick.convert_x_to_numeric
467474
Tick.convert_xunits
475+
Tick.convert_y_to_numeric
468476
Tick.convert_yunits
469477
Tick.draw
470478
Tick.findobj
@@ -529,7 +537,9 @@ Ticks
529537
XTick.add_callback
530538
XTick.axes
531539
XTick.contains
540+
XTick.convert_x_to_numeric
532541
XTick.convert_xunits
542+
XTick.convert_y_to_numeric
533543
XTick.convert_yunits
534544
XTick.draw
535545
XTick.findobj
@@ -594,7 +604,9 @@ Ticks
594604
YTick.add_callback
595605
YTick.axes
596606
YTick.contains
607+
YTick.convert_x_to_numeric
597608
YTick.convert_xunits
609+
YTick.convert_y_to_numeric
598610
YTick.convert_yunits
599611
YTick.draw
600612
YTick.findobj
@@ -669,7 +681,9 @@ Axis
669681
Axis.add_callback
670682
Axis.axes
671683
Axis.contains
684+
Axis.convert_x_to_numeric
672685
Axis.convert_xunits
686+
Axis.convert_y_to_numeric
673687
Axis.convert_yunits
674688
Axis.draw
675689
Axis.findobj
@@ -734,7 +748,9 @@ Axis
734748
XAxis.add_callback
735749
XAxis.axes
736750
XAxis.contains
751+
XAxis.convert_x_to_numeric
737752
XAxis.convert_xunits
753+
XAxis.convert_y_to_numeric
738754
XAxis.convert_yunits
739755
XAxis.draw
740756
XAxis.findobj
@@ -799,7 +815,9 @@ Axis
799815
YAxis.add_callback
800816
YAxis.axes
801817
YAxis.contains
818+
YAxis.convert_x_to_numeric
802819
YAxis.convert_xunits
820+
YAxis.convert_y_to_numeric
803821
YAxis.convert_yunits
804822
YAxis.draw
805823
YAxis.findobj

examples/units/basic_units.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ def rad_fn(x, pos=None):
313313

314314

315315
class BasicUnitConverter(units.ConversionInterface):
316-
@staticmethod
317-
def axisinfo(unit, axis):
316+
@classmethod
317+
def axisinfo(cls, unit, axis):
318318
"""Return AxisInfo instance for x and unit."""
319319

320320
if unit == radians:
@@ -336,9 +336,9 @@ def axisinfo(unit, axis):
336336
return units.AxisInfo(label=unit.unit.fullname)
337337
return None
338338

339-
@staticmethod
340-
def convert(val, unit, axis):
341-
if units.ConversionInterface.is_numlike(val):
339+
@classmethod
340+
def to_numeric(cls, val, unit, axis):
341+
if cls.is_numlike(val):
342342
return val
343343
if np.iterable(val):
344344
if isinstance(val, np.ma.MaskedArray):
@@ -358,8 +358,8 @@ def convert(val, unit, axis):
358358
else:
359359
return val.convert_to(unit).get_value()
360360

361-
@staticmethod
362-
def default_units(x, axis):
361+
@classmethod
362+
def default_units(cls, x, axis):
363363
"""Return the default unit for x or None."""
364364
if np.iterable(x):
365365
for thisx in x:

examples/units/evans_test.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def value(self, unit):
2828

2929

3030
class FooConverter(units.ConversionInterface):
31-
@staticmethod
32-
def axisinfo(unit, axis):
31+
@classmethod
32+
def axisinfo(cls, unit, axis):
3333
"""Return the Foo AxisInfo."""
3434
if unit == 1.0 or unit == 2.0:
3535
return units.AxisInfo(
@@ -41,23 +41,23 @@ def axisinfo(unit, axis):
4141
else:
4242
return None
4343

44-
@staticmethod
45-
def convert(obj, unit, axis):
44+
@classmethod
45+
def to_numeric(cls, obj, unit, axis):
4646
"""
4747
Convert *obj* using *unit*.
4848
4949
If *obj* is a sequence, return the converted sequence.
5050
"""
51-
if units.ConversionInterface.is_numlike(obj):
51+
if cls.is_numlike(obj):
5252
return obj
5353

5454
if np.iterable(obj):
5555
return [o.value(unit) for o in obj]
5656
else:
5757
return obj.value(unit)
5858

59-
@staticmethod
60-
def default_units(x, axis):
59+
@classmethod
60+
def default_units(cls, x, axis):
6161
"""Return the default unit for *x* or None."""
6262
if np.iterable(x):
6363
for thisx in x:

lib/matplotlib/artist.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def have_units(self):
164164
ax = self.axes
165165
return ax and any(axis.have_units() for axis in ax._get_axis_list())
166166

167-
def convert_xunits(self, x):
167+
def convert_x_to_numeric(self, x):
168168
"""
169169
Convert *x* using the unit type of the xaxis.
170170
@@ -174,9 +174,13 @@ def convert_xunits(self, x):
174174
ax = getattr(self, 'axes', None)
175175
if ax is None or ax.xaxis is None:
176176
return x
177-
return ax.xaxis.convert_units(x)
177+
return ax.xaxis.convert_to_numeric(x)
178178

179-
def convert_yunits(self, y):
179+
@cbook.deprecated("3.3", pending=True, alternative="convert_x_to_numeric")
180+
def convert_xunits(self, x):
181+
return self.convert_x_to_numeric(x)
182+
183+
def convert_y_to_numeric(self, y):
180184
"""
181185
Convert *y* using the unit type of the yaxis.
182186
@@ -186,7 +190,11 @@ def convert_yunits(self, y):
186190
ax = getattr(self, 'axes', None)
187191
if ax is None or ax.yaxis is None:
188192
return y
189-
return ax.yaxis.convert_units(y)
193+
return ax.yaxis.convert_to_numeric(y)
194+
195+
@cbook.deprecated("3.3", pending=True, alternative="convert_y_to_numeric")
196+
def convert_yunits(self, y):
197+
return self.convert_y_to_numeric(y)
190198

191199
@property
192200
def axes(self):

0 commit comments

Comments
 (0)