Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ def __call__(self, x, pos=None):
"""
Return the format for tick value *x* at position *pos*.
"""
if len(self.locs) == 0:
if len(self._locs) == 0:
return ''
else:
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
xp = (x - self.offset) / (10. ** self._orderOfMagnitude)
if abs(xp) < 1e-8:
xp = 0
return self._format_maybe_minus_and_locale(self.format, xp)
return self._format_maybe_minus_and_locale(self._format, xp)

def set_scientific(self, b):
"""
Expand Down Expand Up @@ -720,20 +720,20 @@ def get_offset(self):
"""
Return scientific notation, plus offset.
"""
if len(self.locs) == 0:
if len(self._locs) == 0:
return ''
if self.orderOfMagnitude or self.offset:
if self._orderOfMagnitude or self.offset:
offsetStr = ''
sciNotStr = ''
if self.offset:
offsetStr = self.format_data(self.offset)
if self.offset > 0:
offsetStr = '+' + offsetStr
if self.orderOfMagnitude:
if self._orderOfMagnitude:
if self._usetex or self._useMathText:
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
sciNotStr = self.format_data(10 ** self._orderOfMagnitude)
else:
sciNotStr = '1e%d' % self.orderOfMagnitude
sciNotStr = '1e%d' % self._orderOfMagnitude
if self._useMathText or self._usetex:
if sciNotStr != '':
sciNotStr = r'\times\mathdefault{%s}' % sciNotStr
Expand All @@ -745,15 +745,15 @@ def get_offset(self):

def set_locs(self, locs):
# docstring inherited
self.locs = locs
if len(self.locs) > 0:
self._locs = locs
if len(self._locs) > 0:
if self._useOffset:
self._compute_offset()
self._set_order_of_magnitude()
self._set_format()

def _compute_offset(self):
locs = self.locs
locs = self._locs
# Restrict to visible ticks.
vmin, vmax = sorted(self.axis.get_view_interval())
locs = np.asarray(locs)
Expand Down Expand Up @@ -796,19 +796,19 @@ def _set_order_of_magnitude(self):
# if using a numerical offset, find the exponent after applying the
# offset. When lower power limit = upper <> 0, use provided exponent.
if not self._scientific:
self.orderOfMagnitude = 0
self._orderOfMagnitude = 0
return
if self._powerlimits[0] == self._powerlimits[1] != 0:
# fixed scaling when lower power limit = upper <> 0.
self.orderOfMagnitude = self._powerlimits[0]
self._orderOfMagnitude = self._powerlimits[0]
return
# restrict to visible ticks
vmin, vmax = sorted(self.axis.get_view_interval())
locs = np.asarray(self.locs)
locs = np.asarray(self._locs)
locs = locs[(vmin <= locs) & (locs <= vmax)]
locs = np.abs(locs)
if not len(locs):
self.orderOfMagnitude = 0
self._orderOfMagnitude = 0
return
if self.offset:
oom = math.floor(math.log10(vmax - vmin))
Expand All @@ -819,28 +819,28 @@ def _set_order_of_magnitude(self):
else:
oom = math.floor(math.log10(val))
if oom <= self._powerlimits[0]:
self.orderOfMagnitude = oom
self._orderOfMagnitude = oom
elif oom >= self._powerlimits[1]:
self.orderOfMagnitude = oom
self._orderOfMagnitude = oom
else:
self.orderOfMagnitude = 0
self._orderOfMagnitude = 0

def _set_format(self):
# set the format string to format all the ticklabels
if len(self.locs) < 2:
if len(self._locs) < 2:
# Temporarily augment the locations with the axis end points.
_locs = [*self.locs, *self.axis.get_view_interval()]
_locs = [*self._locs, *self.axis.get_view_interval()]
else:
_locs = self.locs
locs = (np.asarray(_locs) - self.offset) / 10. ** self.orderOfMagnitude
_locs = self._locs
locs = (np.asarray(_locs) - self.offset) / 10. ** self._orderOfMagnitude
loc_range = np.ptp(locs)
# Curvilinear coordinates can yield two identical points.
if loc_range == 0:
loc_range = np.max(np.abs(locs))
# Both points might be zero.
if loc_range == 0:
loc_range = 1
if len(self.locs) < 2:
if len(self._locs) < 2:
# We needed the end points only for the loc_range calculation.
locs = locs[:-2]
loc_range_oom = int(math.floor(math.log10(loc_range)))
Expand All @@ -854,9 +854,9 @@ def _set_format(self):
else:
break
sigfigs += 1
self.format = f'%1.{sigfigs}f'
self._format = f'%1.{sigfigs}f'
if self._usetex or self._useMathText:
self.format = r'$\mathdefault{%s}$' % self.format
self._format = r'$\mathdefault{%s}$' % self._format


class LogFormatter(Formatter):
Expand Down Expand Up @@ -1248,7 +1248,7 @@ def set_minor_number(self, minor_number):
self._minor_number = minor_number

def set_locs(self, locs):
self.locs = np.array(locs)
self._locs = np.array(locs)
self._labelled.clear()

if not self._minor:
Expand All @@ -1274,7 +1274,7 @@ def set_locs(self, locs):
# the previous, and between the ticks and the next one. Ticks
# with smallest minimum are chosen. As tiebreak, the ticks
# with smallest sum is chosen.
diff = np.diff(-np.log(1 / self.locs - 1))
diff = np.diff(-np.log(1 / self._locs - 1))
space_pessimistic = np.minimum(
np.concatenate(((np.inf,), diff)),
np.concatenate((diff, (np.inf,))),
Expand All @@ -1284,7 +1284,7 @@ def set_locs(self, locs):
+ np.concatenate((diff, (0,)))
)
good_minor = sorted(
range(len(self.locs)),
range(len(self._locs)),
key=lambda i: (space_pessimistic[i], space_sum[i]),
)[-self._minor_number:]
self._labelled.update(locs[i] for i in good_minor)
Expand Down Expand Up @@ -1335,11 +1335,11 @@ def __call__(self, x, pos=None):
exponent = round(math.log10(1 - x))
s = self._one_minus("10^{%d}" % exponent)
elif x < 0.1:
s = self._format_value(x, self.locs)
s = self._format_value(x, self._locs)
elif x > 0.9:
s = self._one_minus(self._format_value(1-x, 1-self.locs))
s = self._one_minus(self._format_value(1-x, 1-self._locs))
else:
s = self._format_value(x, self.locs, sci_notation=False)
s = self._format_value(x, self._locs, sci_notation=False)
return r"$\mathdefault{%s}$" % s

def format_data_short(self, value):
Expand Down Expand Up @@ -1445,18 +1445,18 @@ def __call__(self, x, pos=None):
If there is no currently offset in the data, it returns the best
engineering formatting that fits the given argument, independently.
"""
if len(self.locs) == 0 or self.offset == 0:
if len(self._locs) == 0 or self.offset == 0:
return self.fix_minus(self.format_data(x))
else:
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
xp = (x - self.offset) / (10. ** self._orderOfMagnitude)
if abs(xp) < 1e-8:
xp = 0
return self._format_maybe_minus_and_locale(self._format, xp)

def set_locs(self, locs):
# docstring inherited
self.locs = locs
if len(self.locs) > 0:
self._locs = locs
if len(self._locs) > 0:
vmin, vmax = sorted(self.axis.get_view_interval())
if self._useOffset:
self._compute_offset()
Expand All @@ -1470,25 +1470,25 @@ def set_locs(self, locs):
# value:
self.offset = round((vmin + vmax)/2, 3)
# Use log1000 to use engineers' oom standards
self.orderOfMagnitude = math.floor(math.log(vmax - vmin, 1000))*3
self._orderOfMagnitude = math.floor(math.log(vmax - vmin, 1000))*3
self._set_format()

# Simplify a bit ScalarFormatter.get_offset: We always want to use
# self.format_data. Also we want to return a non-empty string only if there
# is an offset, no matter what is self.orderOfMagnitude. If there _is_ an
# offset, self.orderOfMagnitude is consulted. This behavior is verified
# is an offset, no matter what is self._orderOfMagnitude. If there _is_ an
# offset, self._orderOfMagnitude is consulted. This behavior is verified
# in `test_ticker.py`.
def get_offset(self):
# docstring inherited
if len(self.locs) == 0:
if len(self._locs) == 0:
return ''
if self.offset:
offsetStr = ''
if self.offset:
offsetStr = self.format_data(self.offset)
if self.offset > 0:
offsetStr = '+' + offsetStr
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
sciNotStr = self.format_data(10 ** self._orderOfMagnitude)
if self._useMathText or self._usetex:
if sciNotStr != '':
sciNotStr = r'\times%s' % sciNotStr
Expand Down Expand Up @@ -1800,8 +1800,8 @@ class FixedLocator(Locator):
"""

def __init__(self, locs, nbins=None):
self.locs = np.asarray(locs)
_api.check_shape((None,), locs=self.locs)
self._locs = np.asarray(locs)
_api.check_shape((None,), locs=self._locs)
self.nbins = max(nbins, 2) if nbins is not None else None

def set_params(self, nbins=None):
Expand All @@ -1821,11 +1821,11 @@ def tick_values(self, vmin, vmax):
Because the values are fixed, *vmin* and *vmax* are not used.
"""
if self.nbins is None:
return self.locs
step = max(int(np.ceil(len(self.locs) / self.nbins)), 1)
ticks = self.locs[::step]
return self._locs
step = max(int(np.ceil(len(self._locs) / self.nbins)), 1)
ticks = self._locs[::step]
for i in range(1, step):
ticks1 = self.locs[i::step]
ticks1 = self._locs[i::step]
if np.abs(ticks1).min() < np.abs(ticks).min():
ticks = ticks1
return self.raise_if_exceeds(ticks)
Expand Down