Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e18ee2

Browse files
authored
Merge pull request #31416 from timhoffm/mnt-deprecate-formatter-attrs
MNT: Privatize Formatter attributes
2 parents 57f697c + 791aaf9 commit 7e18ee2

6 files changed

Lines changed: 73 additions & 53 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Formatter attributes
2+
~~~~~~~~~~~~~~~~~~~~
3+
4+
These following attributes are considered internal and users should not have a need to access them:
5+
6+
- `.ScalarFormatter`: ``orderOfMagnitude`` and ``format``
7+
- `.ConciseDateFormatter`: ``offset_format``
8+
- `.Formatter`: ``locs``

lib/matplotlib/dates.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@ class ConciseDateFormatter(ticker.Formatter):
663663
664664
"""
665665

666+
offset_string = _api.deprecate_privatize_attribute(
667+
"3.11", alternative="get_offset()"
668+
)
669+
666670
def __init__(self, locator, tz=None, formats=None, offset_formats=None,
667671
zero_formats=None, show_offset=True, *, usetex=None):
668672
"""
@@ -717,7 +721,7 @@ def __init__(self, locator, tz=None, formats=None, offset_formats=None,
717721
'%Y-%b-%d',
718722
'%Y-%b-%d',
719723
'%Y-%b-%d %H:%M']
720-
self.offset_string = ''
724+
self._offset_string = ''
721725
self.show_offset = show_offset
722726
self._usetex = mpl._val_or_rc(usetex, 'text.usetex')
723727

@@ -797,21 +801,21 @@ def format_ticks(self, values):
797801
if (self._locator.axis and
798802
self._locator.axis.__name__ in ('xaxis', 'yaxis')
799803
and self._locator.axis.get_inverted()):
800-
self.offset_string = tickdatetime[0].strftime(offsetfmts[level])
804+
self._offset_string = tickdatetime[0].strftime(offsetfmts[level])
801805
else:
802-
self.offset_string = tickdatetime[-1].strftime(offsetfmts[level])
806+
self._offset_string = tickdatetime[-1].strftime(offsetfmts[level])
803807
if self._usetex:
804-
self.offset_string = _wrap_in_tex(self.offset_string)
808+
self._offset_string = _wrap_in_tex(self._offset_string)
805809
else:
806-
self.offset_string = ''
810+
self._offset_string = ''
807811

808812
if self._usetex:
809813
return [_wrap_in_tex(l) for l in labels]
810814
else:
811815
return labels
812816

813817
def get_offset(self):
814-
return self.offset_string
818+
return self._offset_string
815819

816820
def format_data_short(self, value):
817821
return num2date(value, tz=self._tz).strftime('%Y-%m-%d %H:%M:%S')

lib/matplotlib/testing/jpl_units/UnitDblFormatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UnitDblFormatter(ticker.ScalarFormatter):
1414

1515
def __call__(self, x, pos=None):
1616
# docstring inherited
17-
if len(self.locs) == 0:
17+
if len(self._locs) == 0:
1818
return ''
1919
else:
2020
return f'{x:.12}'

lib/matplotlib/tests/test_ticker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def test_scilimits(self, sci_type, scilimits, lim, orderOfMag, fewticks):
920920
ax.yaxis.set_major_locator(mticker.MaxNLocator(4))
921921

922922
tmp_form.set_locs(ax.yaxis.get_majorticklocs())
923-
assert orderOfMag == tmp_form.orderOfMagnitude
923+
assert orderOfMag == tmp_form._orderOfMagnitude
924924

925925
@pytest.mark.parametrize('value, expected', format_data)
926926
def test_format_data(self, value, expected):

lib/matplotlib/ticker.py

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ class Formatter(TickHelper):
213213
"""
214214
# some classes want to see all the locs to help format
215215
# individual ones
216-
locs = []
216+
_locs = []
217+
218+
locs = _api.deprecate_privatize_attribute("3.11")
217219

218220
def __call__(self, x, pos=None):
219221
"""
@@ -293,7 +295,7 @@ def set_locs(self, locs):
293295
This method is called before computing the tick labels because some
294296
formatters need to know all tick locations to do so.
295297
"""
296-
self.locs = locs
298+
self._locs = locs
297299

298300
@staticmethod
299301
def fix_minus(s):
@@ -507,15 +509,18 @@ class ScalarFormatter(Formatter):
507509
508510
"""
509511

512+
orderOfMagnitude = _api.deprecate_privatize_attribute("3.11")
513+
format = _api.deprecate_privatize_attribute("3.11")
514+
510515
def __init__(self, useOffset=None, useMathText=None, useLocale=None, *,
511516
usetex=None):
512517
useOffset = mpl._val_or_rc(useOffset, 'axes.formatter.useoffset')
513518
self._offset_threshold = mpl.rcParams['axes.formatter.offset_threshold']
514519
self.set_useOffset(useOffset)
515520
self.set_usetex(usetex)
516521
self.set_useMathText(useMathText)
517-
self.orderOfMagnitude = 0
518-
self.format = ''
522+
self._orderOfMagnitude = 0
523+
self._format = ''
519524
self._scientific = True
520525
self._powerlimits = mpl.rcParams['axes.formatter.limits']
521526
self.set_useLocale(useLocale)
@@ -664,13 +669,13 @@ def __call__(self, x, pos=None):
664669
"""
665670
Return the format for tick value *x* at position *pos*.
666671
"""
667-
if len(self.locs) == 0:
672+
if len(self._locs) == 0:
668673
return ''
669674
else:
670-
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
675+
xp = (x - self.offset) / (10. ** self._orderOfMagnitude)
671676
if abs(xp) < 1e-8:
672677
xp = 0
673-
return self._format_maybe_minus_and_locale(self.format, xp)
678+
return self._format_maybe_minus_and_locale(self._format, xp)
674679

675680
def set_scientific(self, b):
676681
"""
@@ -764,20 +769,20 @@ def get_offset(self):
764769
"""
765770
Return scientific notation, plus offset.
766771
"""
767-
if len(self.locs) == 0:
772+
if len(self._locs) == 0:
768773
return ''
769-
if self.orderOfMagnitude or self.offset:
774+
if self._orderOfMagnitude or self.offset:
770775
offsetStr = ''
771776
sciNotStr = ''
772777
if self.offset:
773778
offsetStr = self.format_data(self.offset)
774779
if self.offset > 0:
775780
offsetStr = '+' + offsetStr
776-
if self.orderOfMagnitude:
781+
if self._orderOfMagnitude:
777782
if self._usetex or self._useMathText:
778-
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
783+
sciNotStr = self.format_data(10 ** self._orderOfMagnitude)
779784
else:
780-
sciNotStr = '1e%d' % self.orderOfMagnitude
785+
sciNotStr = '1e%d' % self._orderOfMagnitude
781786
if self._useMathText or self._usetex:
782787
if sciNotStr != '':
783788
sciNotStr = r'\times\mathdefault{%s}' % sciNotStr
@@ -789,15 +794,15 @@ def get_offset(self):
789794

790795
def set_locs(self, locs):
791796
# docstring inherited
792-
self.locs = locs
793-
if len(self.locs) > 0:
797+
self._locs = locs
798+
if len(self._locs) > 0:
794799
if self._useOffset:
795800
self._compute_offset()
796801
self._set_order_of_magnitude()
797802
self._set_format()
798803

799804
def _compute_offset(self):
800-
locs = self.locs
805+
locs = self._locs
801806
# Restrict to visible ticks.
802807
vmin, vmax = sorted(self.axis.get_view_interval())
803808
locs = np.asarray(locs)
@@ -840,19 +845,19 @@ def _set_order_of_magnitude(self):
840845
# if using a numerical offset, find the exponent after applying the
841846
# offset. When lower power limit = upper <> 0, use provided exponent.
842847
if not self._scientific:
843-
self.orderOfMagnitude = 0
848+
self._orderOfMagnitude = 0
844849
return
845850
if self._powerlimits[0] == self._powerlimits[1] != 0:
846851
# fixed scaling when lower power limit = upper <> 0.
847-
self.orderOfMagnitude = self._powerlimits[0]
852+
self._orderOfMagnitude = self._powerlimits[0]
848853
return
849854
# restrict to visible ticks
850855
vmin, vmax = sorted(self.axis.get_view_interval())
851-
locs = np.asarray(self.locs)
856+
locs = np.asarray(self._locs)
852857
locs = locs[(vmin <= locs) & (locs <= vmax)]
853858
locs = np.abs(locs)
854859
if not len(locs):
855-
self.orderOfMagnitude = 0
860+
self._orderOfMagnitude = 0
856861
return
857862
if self.offset:
858863
oom = math.floor(math.log10(vmax - vmin))
@@ -863,28 +868,28 @@ def _set_order_of_magnitude(self):
863868
else:
864869
oom = math.floor(math.log10(val))
865870
if oom <= self._powerlimits[0]:
866-
self.orderOfMagnitude = oom
871+
self._orderOfMagnitude = oom
867872
elif oom >= self._powerlimits[1]:
868-
self.orderOfMagnitude = oom
873+
self._orderOfMagnitude = oom
869874
else:
870-
self.orderOfMagnitude = 0
875+
self._orderOfMagnitude = 0
871876

872877
def _set_format(self):
873878
# set the format string to format all the ticklabels
874-
if len(self.locs) < 2:
879+
if len(self._locs) < 2:
875880
# Temporarily augment the locations with the axis end points.
876-
_locs = [*self.locs, *self.axis.get_view_interval()]
881+
_locs = [*self._locs, *self.axis.get_view_interval()]
877882
else:
878-
_locs = self.locs
879-
locs = (np.asarray(_locs) - self.offset) / 10. ** self.orderOfMagnitude
883+
_locs = self._locs
884+
locs = (np.asarray(_locs) - self.offset) / 10. ** self._orderOfMagnitude
880885
loc_range = np.ptp(locs)
881886
# Curvilinear coordinates can yield two identical points.
882887
if loc_range == 0:
883888
loc_range = np.max(np.abs(locs))
884889
# Both points might be zero.
885890
if loc_range == 0:
886891
loc_range = 1
887-
if len(self.locs) < 2:
892+
if len(self._locs) < 2:
888893
# We needed the end points only for the loc_range calculation.
889894
locs = locs[:-2]
890895
loc_range_oom = int(math.floor(math.log10(loc_range)))
@@ -898,9 +903,9 @@ def _set_format(self):
898903
else:
899904
break
900905
sigfigs += 1
901-
self.format = f'%1.{sigfigs}f'
906+
self._format = f'%1.{sigfigs}f'
902907
if self._usetex or self._useMathText:
903-
self.format = r'$\mathdefault{%s}$' % self.format
908+
self._format = r'$\mathdefault{%s}$' % self._format
904909

905910

906911
class LogFormatter(Formatter):
@@ -1292,7 +1297,7 @@ def set_minor_number(self, minor_number):
12921297
self._minor_number = minor_number
12931298

12941299
def set_locs(self, locs):
1295-
self.locs = np.array(locs)
1300+
self._locs = np.array(locs)
12961301
self._labelled.clear()
12971302

12981303
if not self._minor:
@@ -1318,7 +1323,7 @@ def set_locs(self, locs):
13181323
# the previous, and between the ticks and the next one. Ticks
13191324
# with smallest minimum are chosen. As tiebreak, the ticks
13201325
# with smallest sum is chosen.
1321-
diff = np.diff(-np.log(1 / self.locs - 1))
1326+
diff = np.diff(-np.log(1 / self._locs - 1))
13221327
space_pessimistic = np.minimum(
13231328
np.concatenate(((np.inf,), diff)),
13241329
np.concatenate((diff, (np.inf,))),
@@ -1328,7 +1333,7 @@ def set_locs(self, locs):
13281333
+ np.concatenate((diff, (0,)))
13291334
)
13301335
good_minor = sorted(
1331-
range(len(self.locs)),
1336+
range(len(self._locs)),
13321337
key=lambda i: (space_pessimistic[i], space_sum[i]),
13331338
)[-self._minor_number:]
13341339
self._labelled.update(locs[i] for i in good_minor)
@@ -1379,11 +1384,11 @@ def __call__(self, x, pos=None):
13791384
exponent = round(math.log10(1 - x))
13801385
s = self._one_minus("10^{%d}" % exponent)
13811386
elif x < 0.1:
1382-
s = self._format_value(x, self.locs)
1387+
s = self._format_value(x, self._locs)
13831388
elif x > 0.9:
1384-
s = self._one_minus(self._format_value(1-x, 1-self.locs))
1389+
s = self._one_minus(self._format_value(1-x, 1-self._locs))
13851390
else:
1386-
s = self._format_value(x, self.locs, sci_notation=False)
1391+
s = self._format_value(x, self._locs, sci_notation=False)
13871392
return r"$\mathdefault{%s}$" % s
13881393

13891394
def format_data_short(self, value):
@@ -1489,18 +1494,18 @@ def __call__(self, x, pos=None):
14891494
If there is no currently offset in the data, it returns the best
14901495
engineering formatting that fits the given argument, independently.
14911496
"""
1492-
if len(self.locs) == 0 or self.offset == 0:
1497+
if len(self._locs) == 0 or self.offset == 0:
14931498
return self.fix_minus(self.format_data(x))
14941499
else:
1495-
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
1500+
xp = (x - self.offset) / (10. ** self._orderOfMagnitude)
14961501
if abs(xp) < 1e-8:
14971502
xp = 0
1498-
return self._format_maybe_minus_and_locale(self.format, xp)
1503+
return self._format_maybe_minus_and_locale(self._format, xp)
14991504

15001505
def set_locs(self, locs):
15011506
# docstring inherited
1502-
self.locs = locs
1503-
if len(self.locs) > 0:
1507+
self._locs = locs
1508+
if len(self._locs) > 0:
15041509
vmin, vmax = sorted(self.axis.get_view_interval())
15051510
if self._useOffset:
15061511
self._compute_offset()
@@ -1514,25 +1519,25 @@ def set_locs(self, locs):
15141519
# value:
15151520
self.offset = round((vmin + vmax)/2, 3)
15161521
# Use log1000 to use engineers' oom standards
1517-
self.orderOfMagnitude = math.floor(math.log(vmax - vmin, 1000))*3
1522+
self._orderOfMagnitude = math.floor(math.log(vmax - vmin, 1000))*3
15181523
self._set_format()
15191524

15201525
# Simplify a bit ScalarFormatter.get_offset: We always want to use
15211526
# self.format_data. Also we want to return a non-empty string only if there
1522-
# is an offset, no matter what is self.orderOfMagnitude. If there _is_ an
1523-
# offset, self.orderOfMagnitude is consulted. This behavior is verified
1527+
# is an offset, no matter what is self._orderOfMagnitude. If there _is_ an
1528+
# offset, self._orderOfMagnitude is consulted. This behavior is verified
15241529
# in `test_ticker.py`.
15251530
def get_offset(self):
15261531
# docstring inherited
1527-
if len(self.locs) == 0:
1532+
if len(self._locs) == 0:
15281533
return ''
15291534
if self.offset:
15301535
offsetStr = ''
15311536
if self.offset:
15321537
offsetStr = self.format_data(self.offset)
15331538
if self.offset > 0:
15341539
offsetStr = '+' + offsetStr
1535-
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
1540+
sciNotStr = self.format_data(10 ** self._orderOfMagnitude)
15361541
if self._useMathText or self._usetex:
15371542
if sciNotStr != '':
15381543
sciNotStr = r'\times%s' % sciNotStr

lib/matplotlib/ticker.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class TickHelper:
2424

2525
class Formatter(TickHelper):
2626
locs: list[float]
27+
_locs: list[float]
2728
def __call__(self, x: float, pos: int | None = ...) -> str: ...
2829
def format_ticks(self, values: list[float]) -> list[str]: ...
2930
def format_data(self, value: float) -> str: ...
@@ -58,7 +59,9 @@ class StrMethodFormatter(Formatter):
5859

5960
class ScalarFormatter(Formatter):
6061
orderOfMagnitude: int
62+
_orderOfMagnitude: int
6163
format: str
64+
_format: str
6265
def __init__(
6366
self,
6467
useOffset: bool | float | None = ...,

0 commit comments

Comments
 (0)