diff --git a/galleries/users_explain/artists/transforms_tutorial.py b/galleries/users_explain/artists/transforms_tutorial.py index 3920fe886c7f..1a25f1f87c88 100644 --- a/galleries/users_explain/artists/transforms_tutorial.py +++ b/galleries/users_explain/artists/transforms_tutorial.py @@ -64,10 +64,9 @@ | |is top right of the output in | | | |"display units". | | | | | | -| |The exact interpretation of the | | -| |units depends on the back end. For | | -| |example it is pixels for Agg and | | -| |points for svg/pdf. | | +| |"Display units" depends on the | | +| |backend. For example, Agg uses | | +| |pixels, and SVG/PDF use points. | | +----------------+-----------------------------------+-----------------------------+ The `~matplotlib.transforms.Transform` objects are naive to the source and diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 19ddbb6d0883..2258c63e1bc2 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -8,6 +8,7 @@ import copy import enum import functools +import itertools import logging import math import os @@ -409,14 +410,14 @@ def get_xheight(self, fontname: str, fontsize: float, dpi: float) -> float: metrics = self.get_metrics( fontname, mpl.rcParams['mathtext.default'], 'x', fontsize, dpi) return metrics.iceberg - xHeight = (pclt['xHeight'] / 64.0) * (fontsize / 12.0) * (dpi / 100.0) - return xHeight + x_height = (pclt['xHeight'] / 64) * (fontsize / 12) * (dpi / 100) + return x_height def get_underline_thickness(self, font: str, fontsize: float, dpi: float) -> float: # This function used to grab underline thickness from the font # metrics, but that information is just too un-reliable, so it # is now hardcoded. - return ((0.75 / 12.0) * fontsize * dpi) / 72.0 + return ((0.75 / 12) * fontsize * dpi) / 72 def get_kern(self, font1: str, fontclass1: str, sym1: str, fontsize1: float, font2: str, fontclass2: str, sym2: str, fontsize2: float, @@ -1226,21 +1227,13 @@ def kern(self) -> None: linked list. """ new_children = [] - num_children = len(self.children) - if num_children: - for i in range(num_children): - elem = self.children[i] - if i < num_children - 1: - next = self.children[i + 1] - else: - next = None - - new_children.append(elem) - kerning_distance = elem.get_kerning(next) - if kerning_distance != 0.: - kern = Kern(kerning_distance) - new_children.append(kern) - self.children = new_children + for elem0, elem1 in itertools.zip_longest(self.children, self.children[1:]): + new_children.append(elem0) + kerning_distance = elem0.get_kerning(elem1) + if kerning_distance != 0.: + kern = Kern(kerning_distance) + new_children.append(kern) + self.children = new_children def hpack(self, w: float = 0.0, m: T.Literal['additional', 'exactly'] = 'additional') -> None: @@ -1534,11 +1527,9 @@ class AutoHeightChar(Hlist): def __init__(self, c: str, height: float, depth: float, state: ParserState, always: bool = False, factor: float | None = None): - alternatives = state.fontset.get_sized_alternatives_for_symbol( - state.font, c) + alternatives = state.fontset.get_sized_alternatives_for_symbol(state.font, c) - xHeight = state.fontset.get_xheight( - state.font, state.fontsize, state.dpi) + x_height = state.fontset.get_xheight(state.font, state.fontsize, state.dpi) state = state.copy() target_total = height + depth @@ -1546,8 +1537,8 @@ def __init__(self, c: str, height: float, depth: float, state: ParserState, state.font = fontname char = Char(sym, state) # Ensure that size 0 is chosen when the text is regular sized but - # with descender glyphs by subtracting 0.2 * xHeight - if char.height + char.depth >= target_total - 0.2 * xHeight: + # with descender glyphs by subtracting 0.2 * x_height + if char.height + char.depth >= target_total - 0.2 * x_height: break shift = 0.0 @@ -1574,8 +1565,7 @@ class AutoWidthChar(Hlist): def __init__(self, c: str, width: float, state: ParserState, always: bool = False, char_class: type[Char] = Char): - alternatives = state.fontset.get_sized_alternatives_for_symbol( - state.font, c) + alternatives = state.fontset.get_sized_alternatives_for_symbol(state.font, c) state = state.copy() for fontname, sym in alternatives: @@ -2468,7 +2458,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any: state = self.get_state() rule_thickness = state.fontset.get_underline_thickness( state.font, state.fontsize, state.dpi) - xHeight = state.fontset.get_xheight( + x_height = state.fontset.get_xheight( state.font, state.fontsize, state.dpi) if napostrophes: @@ -2537,24 +2527,21 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any: nucleus = Hlist([nucleus]) # Handle regular sub/superscripts - constants = _get_font_constant_set(state) + consts = _get_font_constant_set(state) lc_height = last_char.height lc_baseline = 0 if self.is_dropsub(last_char): lc_baseline = last_char.depth # Compute kerning for sub and super - superkern = constants.delta * xHeight - subkern = constants.delta * xHeight + superkern = consts.delta * x_height + subkern = consts.delta * x_height if self.is_slanted(last_char): - superkern += constants.delta * xHeight - superkern += (constants.delta_slanted * - (lc_height - xHeight * 2. / 3.)) + superkern += consts.delta * x_height + superkern += consts.delta_slanted * (lc_height - x_height * 2 / 3) if self.is_dropsub(last_char): - subkern = (3 * constants.delta - - constants.delta_integral) * lc_height - superkern = (3 * constants.delta + - constants.delta_integral) * lc_height + subkern = (3 * consts.delta - consts.delta_integral) * lc_height + superkern = (3 * consts.delta + consts.delta_integral) * lc_height else: subkern = 0 @@ -2567,28 +2554,28 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any: x = Hlist([Kern(subkern), T.cast(Node, sub)]) x.shrink() if self.is_dropsub(last_char): - shift_down = lc_baseline + constants.subdrop * xHeight + shift_down = lc_baseline + consts.subdrop * x_height else: - shift_down = constants.sub1 * xHeight + shift_down = consts.sub1 * x_height x.shift_amount = shift_down else: x = Hlist([Kern(superkern), super]) x.shrink() if self.is_dropsub(last_char): - shift_up = lc_height - constants.subdrop * xHeight + shift_up = lc_height - consts.subdrop * x_height else: - shift_up = constants.sup1 * xHeight + shift_up = consts.sup1 * x_height if sub is None: x.shift_amount = -shift_up else: # Both sub and superscript y = Hlist([Kern(subkern), sub]) y.shrink() if self.is_dropsub(last_char): - shift_down = lc_baseline + constants.subdrop * xHeight + shift_down = lc_baseline + consts.subdrop * x_height else: - shift_down = constants.sub2 * xHeight + shift_down = consts.sub2 * x_height # If sub and superscript collide, move super up - clr = (2.0 * rule_thickness - + clr = (2 * rule_thickness - ((shift_up - x.depth) - (y.height - shift_down))) if clr > 0.: shift_up += clr @@ -2599,7 +2586,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any: x.shift_amount = shift_down if not self.is_dropsub(last_char): - x.width += constants.script_space * xHeight + x.width += consts.script_space * x_height # Do we need to add a space after the nucleus? # To find out, check the flag set by operatorname @@ -2624,12 +2611,13 @@ def _genfrac(self, ldelim: str, rdelim: str, rule: float | None, style: _MathSty width = max(num.width, den.width) cnum.hpack(width, 'exactly') cden.hpack(width, 'exactly') - vlist = Vlist([cnum, # numerator - Vbox(0, thickness * 2.0), # space - Hrule(state, rule), # rule - Vbox(0, thickness * 2.0), # space - cden # denominator - ]) + vlist = Vlist([ + cnum, # numerator + Vbox(0, 2 * thickness), # space + Hrule(state, rule), # rule + Vbox(0, 2 * thickness), # space + cden, # denominator + ]) # Shift so the fraction line sits in the middle of the # equals sign @@ -2637,20 +2625,12 @@ def _genfrac(self, ldelim: str, rdelim: str, rule: float | None, style: _MathSty state.font, mpl.rcParams['mathtext.default'], '=', state.fontsize, state.dpi) shift = (cden.height - - ((metrics.ymax + metrics.ymin) / 2 - - thickness * 3.0)) + ((metrics.ymax + metrics.ymin) / 2 - 3 * thickness)) vlist.shift_amount = shift - result = [Hlist([vlist, Hbox(thickness * 2.)])] + result: list[Box | Char | str] = [Hlist([vlist, Hbox(2 * thickness)])] if ldelim or rdelim: - if ldelim == '': - ldelim = '.' - if rdelim == '': - rdelim = '.' - return self._auto_sized_delimiter(ldelim, - T.cast(list[Box | Char | str], - result), - rdelim) + return self._auto_sized_delimiter(ldelim or ".", result, rdelim or ".") return result def style_literal(self, toks: ParseResults) -> T.Any: @@ -2719,7 +2699,7 @@ def sqrt(self, toks: ParseResults) -> T.Any: # Determine the height of the body, and add a little extra to # the height so it doesn't seem cramped - height = body.height - body.shift_amount + thickness * 5.0 + height = body.height - body.shift_amount + 5 * thickness depth = body.depth + body.shift_amount check = AutoHeightChar(r'\__sqrt__', height, depth, state, always=True) height = check.height - check.shift_amount @@ -2729,13 +2709,13 @@ def sqrt(self, toks: ParseResults) -> T.Any: padded_body = Hlist([Hbox(2 * thickness), body, Hbox(2 * thickness)]) rightside = Vlist([Hrule(state), Glue('fill'), padded_body]) # Stretch the glue between the hrule and the body - rightside.vpack(height + (state.fontsize * state.dpi) / (100.0 * 12.0), + rightside.vpack(height + (state.fontsize * state.dpi) / (100 * 12), 'exactly', depth) # Add the root and shift it upward so it is above the tick. # The value of 0.6 is a hard-coded hack ;) if not root: - root = Box(check.width * 0.5, 0., 0.) + root = Box(0.5 * check.width, 0., 0.) else: root = Hlist(root) root.shrink() @@ -2744,11 +2724,12 @@ def sqrt(self, toks: ParseResults) -> T.Any: root_vlist = Vlist([Hlist([root])]) root_vlist.shift_amount = -height * 0.6 - hlist = Hlist([root_vlist, # Root - # Negative kerning to put root over tick - Kern(-check.width * 0.5), - check, # Check - rightside]) # Body + hlist = Hlist([ + root_vlist, # Root + Kern(-0.5 * check.width), # Negative kerning to put root over tick + check, # Check + rightside, # Body + ]) return [hlist] def overline(self, toks: ParseResults) -> T.Any: @@ -2757,14 +2738,14 @@ def overline(self, toks: ParseResults) -> T.Any: state = self.get_state() thickness = state.get_current_underline_thickness() - height = body.height - body.shift_amount + thickness * 3.0 + height = body.height - body.shift_amount + 3 * thickness depth = body.depth + body.shift_amount # Place overline above body rightside = Vlist([Hrule(state), Glue('fill'), Hlist([body])]) # Stretch the glue between the hrule and the body - rightside.vpack(height + (state.fontsize * state.dpi) / (100.0 * 12.0), + rightside.vpack(height + (state.fontsize * state.dpi) / (100 * 12), 'exactly', depth) hlist = Hlist([rightside]) @@ -2810,10 +2791,7 @@ def _auto_sized_delimiter(self, front: str, def auto_delim(self, toks: ParseResults) -> T.Any: return self._auto_sized_delimiter( - toks["left"], - # if "mid" in toks ... can be removed when requiring pyparsing 3. - toks["mid"].as_list() if "mid" in toks else [], - toks["right"]) + toks["left"], toks["mid"].as_list(), toks["right"]) def boldsymbol(self, toks: ParseResults) -> T.Any: self.push_state() diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index fafdf92017f2..d80e7b4dafb9 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1330,10 +1330,8 @@ def _update_ticks(self): return ticks_to_draw - def _get_ticklabel_bboxes(self, ticks, renderer=None): + def _get_ticklabel_bboxes(self, ticks, renderer): """Return lists of bboxes for ticks' label1's and label2's.""" - if renderer is None: - renderer = self.get_figure(root=True)._get_renderer() return ([tick.label1.get_window_extent(renderer) for tick in ticks if tick.label1.get_visible()], [tick.label2.get_window_extent(renderer) diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index 00b400e0977b..1d7c62f154f6 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -207,7 +207,7 @@ def transform_non_affine(self, values): # docstring inherited x, y = values.T r = np.hypot(x, y) - theta = (np.arctan2(y, x) + 2 * np.pi) % (2 * np.pi) + theta = np.arctan2(y, x) % (2 * np.pi) if self._use_rmin and self._axis is not None: r += self._axis.get_rorigin() r *= self._axis.get_rsign()