Skip to content

Commit 791aaf9

Browse files
MNT: Privatize Formatter attributes
Co-authored-by: buddy0452004 <rahulmonani2004@gmail.com>
1 parent 49fd492 commit 791aaf9

File tree

6 files changed

+73
-53
lines changed

6 files changed

+73
-53
lines changed
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
@@ -665,6 +665,10 @@ class ConciseDateFormatter(ticker.Formatter):
665665
666666
"""
667667

668+
offset_string = _api.deprecate_privatize_attribute(
669+
"3.11", alternative="get_offset()"
670+
)
671+
668672
def __init__(self, locator, tz=None, formats=None, offset_formats=None,
669673
zero_formats=None, show_offset=True, *, usetex=None):
670674
"""
@@ -719,7 +723,7 @@ def __init__(self, locator, tz=None, formats=None, offset_formats=None,
719723
'%Y-%b-%d',
720724
'%Y-%b-%d',
721725
'%Y-%b-%d %H:%M']
722-
self.offset_string = ''
726+
self._offset_string = ''
723727
self.show_offset = show_offset
724728
self._usetex = mpl._val_or_rc(usetex, 'text.usetex')
725729

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

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

815819
def get_offset(self):
816-
return self.offset_string
820+
return self._offset_string
817821

818822
def format_data_short(self, value):
819823
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
@@ -202,7 +202,9 @@ class Formatter(TickHelper):
202202
"""
203203
# some classes want to see all the locs to help format
204204
# individual ones
205-
locs = []
205+
_locs = []
206+
207+
locs = _api.deprecate_privatize_attribute("3.11")
206208

207209
def __call__(self, x, pos=None):
208210
"""
@@ -250,7 +252,7 @@ def set_locs(self, locs):
250252
This method is called before computing the tick labels because some
251253
formatters need to know all tick locations to do so.
252254
"""
253-
self.locs = locs
255+
self._locs = locs
254256

255257
@staticmethod
256258
def fix_minus(s):
@@ -458,15 +460,18 @@ class ScalarFormatter(Formatter):
458460
459461
"""
460462

463+
orderOfMagnitude = _api.deprecate_privatize_attribute("3.11")
464+
format = _api.deprecate_privatize_attribute("3.11")
465+
461466
def __init__(self, useOffset=None, useMathText=None, useLocale=None, *,
462467
usetex=None):
463468
useOffset = mpl._val_or_rc(useOffset, 'axes.formatter.useoffset')
464469
self._offset_threshold = mpl.rcParams['axes.formatter.offset_threshold']
465470
self.set_useOffset(useOffset)
466471
self.set_usetex(usetex)
467472
self.set_useMathText(useMathText)
468-
self.orderOfMagnitude = 0
469-
self.format = ''
473+
self._orderOfMagnitude = 0
474+
self._format = ''
470475
self._scientific = True
471476
self._powerlimits = mpl.rcParams['axes.formatter.limits']
472477
self.set_useLocale(useLocale)
@@ -615,13 +620,13 @@ def __call__(self, x, pos=None):
615620
"""
616621
Return the format for tick value *x* at position *pos*.
617622
"""
618-
if len(self.locs) == 0:
623+
if len(self._locs) == 0:
619624
return ''
620625
else:
621-
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
626+
xp = (x - self.offset) / (10. ** self._orderOfMagnitude)
622627
if abs(xp) < 1e-8:
623628
xp = 0
624-
return self._format_maybe_minus_and_locale(self.format, xp)
629+
return self._format_maybe_minus_and_locale(self._format, xp)
625630

626631
def set_scientific(self, b):
627632
"""
@@ -715,20 +720,20 @@ def get_offset(self):
715720
"""
716721
Return scientific notation, plus offset.
717722
"""
718-
if len(self.locs) == 0:
723+
if len(self._locs) == 0:
719724
return ''
720-
if self.orderOfMagnitude or self.offset:
725+
if self._orderOfMagnitude or self.offset:
721726
offsetStr = ''
722727
sciNotStr = ''
723728
if self.offset:
724729
offsetStr = self.format_data(self.offset)
725730
if self.offset > 0:
726731
offsetStr = '+' + offsetStr
727-
if self.orderOfMagnitude:
732+
if self._orderOfMagnitude:
728733
if self._usetex or self._useMathText:
729-
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
734+
sciNotStr = self.format_data(10 ** self._orderOfMagnitude)
730735
else:
731-
sciNotStr = '1e%d' % self.orderOfMagnitude
736+
sciNotStr = '1e%d' % self._orderOfMagnitude
732737
if self._useMathText or self._usetex:
733738
if sciNotStr != '':
734739
sciNotStr = r'\times\mathdefault{%s}' % sciNotStr
@@ -740,15 +745,15 @@ def get_offset(self):
740745

741746
def set_locs(self, locs):
742747
# docstring inherited
743-
self.locs = locs
744-
if len(self.locs) > 0:
748+
self._locs = locs
749+
if len(self._locs) > 0:
745750
if self._useOffset:
746751
self._compute_offset()
747752
self._set_order_of_magnitude()
748753
self._set_format()
749754

750755
def _compute_offset(self):
751-
locs = self.locs
756+
locs = self._locs
752757
# Restrict to visible ticks.
753758
vmin, vmax = sorted(self.axis.get_view_interval())
754759
locs = np.asarray(locs)
@@ -791,19 +796,19 @@ def _set_order_of_magnitude(self):
791796
# if using a numerical offset, find the exponent after applying the
792797
# offset. When lower power limit = upper <> 0, use provided exponent.
793798
if not self._scientific:
794-
self.orderOfMagnitude = 0
799+
self._orderOfMagnitude = 0
795800
return
796801
if self._powerlimits[0] == self._powerlimits[1] != 0:
797802
# fixed scaling when lower power limit = upper <> 0.
798-
self.orderOfMagnitude = self._powerlimits[0]
803+
self._orderOfMagnitude = self._powerlimits[0]
799804
return
800805
# restrict to visible ticks
801806
vmin, vmax = sorted(self.axis.get_view_interval())
802-
locs = np.asarray(self.locs)
807+
locs = np.asarray(self._locs)
803808
locs = locs[(vmin <= locs) & (locs <= vmax)]
804809
locs = np.abs(locs)
805810
if not len(locs):
806-
self.orderOfMagnitude = 0
811+
self._orderOfMagnitude = 0
807812
return
808813
if self.offset:
809814
oom = math.floor(math.log10(vmax - vmin))
@@ -814,28 +819,28 @@ def _set_order_of_magnitude(self):
814819
else:
815820
oom = math.floor(math.log10(val))
816821
if oom <= self._powerlimits[0]:
817-
self.orderOfMagnitude = oom
822+
self._orderOfMagnitude = oom
818823
elif oom >= self._powerlimits[1]:
819-
self.orderOfMagnitude = oom
824+
self._orderOfMagnitude = oom
820825
else:
821-
self.orderOfMagnitude = 0
826+
self._orderOfMagnitude = 0
822827

823828
def _set_format(self):
824829
# set the format string to format all the ticklabels
825-
if len(self.locs) < 2:
830+
if len(self._locs) < 2:
826831
# Temporarily augment the locations with the axis end points.
827-
_locs = [*self.locs, *self.axis.get_view_interval()]
832+
_locs = [*self._locs, *self.axis.get_view_interval()]
828833
else:
829-
_locs = self.locs
830-
locs = (np.asarray(_locs) - self.offset) / 10. ** self.orderOfMagnitude
834+
_locs = self._locs
835+
locs = (np.asarray(_locs) - self.offset) / 10. ** self._orderOfMagnitude
831836
loc_range = np.ptp(locs)
832837
# Curvilinear coordinates can yield two identical points.
833838
if loc_range == 0:
834839
loc_range = np.max(np.abs(locs))
835840
# Both points might be zero.
836841
if loc_range == 0:
837842
loc_range = 1
838-
if len(self.locs) < 2:
843+
if len(self._locs) < 2:
839844
# We needed the end points only for the loc_range calculation.
840845
locs = locs[:-2]
841846
loc_range_oom = int(math.floor(math.log10(loc_range)))
@@ -849,9 +854,9 @@ def _set_format(self):
849854
else:
850855
break
851856
sigfigs += 1
852-
self.format = f'%1.{sigfigs}f'
857+
self._format = f'%1.{sigfigs}f'
853858
if self._usetex or self._useMathText:
854-
self.format = r'$\mathdefault{%s}$' % self.format
859+
self._format = r'$\mathdefault{%s}$' % self._format
855860

856861

857862
class LogFormatter(Formatter):
@@ -1243,7 +1248,7 @@ def set_minor_number(self, minor_number):
12431248
self._minor_number = minor_number
12441249

12451250
def set_locs(self, locs):
1246-
self.locs = np.array(locs)
1251+
self._locs = np.array(locs)
12471252
self._labelled.clear()
12481253

12491254
if not self._minor:
@@ -1269,7 +1274,7 @@ def set_locs(self, locs):
12691274
# the previous, and between the ticks and the next one. Ticks
12701275
# with smallest minimum are chosen. As tiebreak, the ticks
12711276
# with smallest sum is chosen.
1272-
diff = np.diff(-np.log(1 / self.locs - 1))
1277+
diff = np.diff(-np.log(1 / self._locs - 1))
12731278
space_pessimistic = np.minimum(
12741279
np.concatenate(((np.inf,), diff)),
12751280
np.concatenate((diff, (np.inf,))),
@@ -1279,7 +1284,7 @@ def set_locs(self, locs):
12791284
+ np.concatenate((diff, (0,)))
12801285
)
12811286
good_minor = sorted(
1282-
range(len(self.locs)),
1287+
range(len(self._locs)),
12831288
key=lambda i: (space_pessimistic[i], space_sum[i]),
12841289
)[-self._minor_number:]
12851290
self._labelled.update(locs[i] for i in good_minor)
@@ -1330,11 +1335,11 @@ def __call__(self, x, pos=None):
13301335
exponent = round(math.log10(1 - x))
13311336
s = self._one_minus("10^{%d}" % exponent)
13321337
elif x < 0.1:
1333-
s = self._format_value(x, self.locs)
1338+
s = self._format_value(x, self._locs)
13341339
elif x > 0.9:
1335-
s = self._one_minus(self._format_value(1-x, 1-self.locs))
1340+
s = self._one_minus(self._format_value(1-x, 1-self._locs))
13361341
else:
1337-
s = self._format_value(x, self.locs, sci_notation=False)
1342+
s = self._format_value(x, self._locs, sci_notation=False)
13381343
return r"$\mathdefault{%s}$" % s
13391344

13401345
def format_data_short(self, value):
@@ -1440,18 +1445,18 @@ def __call__(self, x, pos=None):
14401445
If there is no currently offset in the data, it returns the best
14411446
engineering formatting that fits the given argument, independently.
14421447
"""
1443-
if len(self.locs) == 0 or self.offset == 0:
1448+
if len(self._locs) == 0 or self.offset == 0:
14441449
return self.fix_minus(self.format_data(x))
14451450
else:
1446-
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
1451+
xp = (x - self.offset) / (10. ** self._orderOfMagnitude)
14471452
if abs(xp) < 1e-8:
14481453
xp = 0
1449-
return self._format_maybe_minus_and_locale(self.format, xp)
1454+
return self._format_maybe_minus_and_locale(self._format, xp)
14501455

14511456
def set_locs(self, locs):
14521457
# docstring inherited
1453-
self.locs = locs
1454-
if len(self.locs) > 0:
1458+
self._locs = locs
1459+
if len(self._locs) > 0:
14551460
vmin, vmax = sorted(self.axis.get_view_interval())
14561461
if self._useOffset:
14571462
self._compute_offset()
@@ -1465,25 +1470,25 @@ def set_locs(self, locs):
14651470
# value:
14661471
self.offset = round((vmin + vmax)/2, 3)
14671472
# Use log1000 to use engineers' oom standards
1468-
self.orderOfMagnitude = math.floor(math.log(vmax - vmin, 1000))*3
1473+
self._orderOfMagnitude = math.floor(math.log(vmax - vmin, 1000))*3
14691474
self._set_format()
14701475

14711476
# Simplify a bit ScalarFormatter.get_offset: We always want to use
14721477
# self.format_data. Also we want to return a non-empty string only if there
1473-
# is an offset, no matter what is self.orderOfMagnitude. If there _is_ an
1474-
# offset, self.orderOfMagnitude is consulted. This behavior is verified
1478+
# is an offset, no matter what is self._orderOfMagnitude. If there _is_ an
1479+
# offset, self._orderOfMagnitude is consulted. This behavior is verified
14751480
# in `test_ticker.py`.
14761481
def get_offset(self):
14771482
# docstring inherited
1478-
if len(self.locs) == 0:
1483+
if len(self._locs) == 0:
14791484
return ''
14801485
if self.offset:
14811486
offsetStr = ''
14821487
if self.offset:
14831488
offsetStr = self.format_data(self.offset)
14841489
if self.offset > 0:
14851490
offsetStr = '+' + offsetStr
1486-
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
1491+
sciNotStr = self.format_data(10 ** self._orderOfMagnitude)
14871492
if self._useMathText or self._usetex:
14881493
if sciNotStr != '':
14891494
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)