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

Skip to content

Ensure log formatters use Unicode minus #22140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
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
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/errorbar_mixed.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/errorbar_mixed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,390 changes: 1,208 additions & 1,182 deletions lib/matplotlib/tests/baseline_images/test_axes/errorbar_mixed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,9 @@ def test_pyplot_axes():

@image_comparison(['log_scales'])
def test_log_scales():
# Remove this if regenerating the image.
plt.rcParams['axes.unicode_minus'] = False

fig, ax = plt.subplots()
ax.plot(np.log(np.linspace(0.1, 100)))
ax.set_yscale('log', base=5.5)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def test_colorbar_format(fmt):
im.set_norm(LogNorm(vmin=0.1, vmax=10))
fig.canvas.draw()
assert (cbar.ax.yaxis.get_ticklabels()[0].get_text() ==
r'$\mathdefault{10^{-2}}$')
'$\\mathdefault{10^{\N{Minus Sign}2}}$')


def test_colorbar_scale_reset():
Expand Down
17 changes: 10 additions & 7 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ def test_basic(self, labelOnlyBase, base, exponent, locs, positions,
formatter.axis = FakeAxis(1, base**exponent)
vals = base**locs
labels = [formatter(x, pos) for (x, pos) in zip(vals, positions)]
expected = [label.replace('-', '\N{Minus Sign}') for label in expected]
assert labels == expected

def test_blank(self):
Expand All @@ -662,7 +663,7 @@ class TestLogFormatterMathtext:
@pytest.mark.parametrize('min_exponent, value, expected', test_data)
def test_min_exponent(self, min_exponent, value, expected):
with mpl.rc_context({'axes.formatter.min_exponent': min_exponent}):
assert self.fmt(value) == expected
assert self.fmt(value) == expected.replace('-', '\N{Minus Sign}')


class TestLogFormatterSciNotation:
Expand Down Expand Up @@ -691,7 +692,7 @@ def test_basic(self, base, value, expected):
formatter = mticker.LogFormatterSciNotation(base=base)
formatter.sublabel = {1, 2, 5, 1.2}
with mpl.rc_context({'text.usetex': False}):
assert formatter(value) == expected
assert formatter(value) == expected.replace('-', '\N{Minus Sign}')


class TestLogFormatter:
Expand Down Expand Up @@ -907,19 +908,21 @@ def logit_deformatter(string):
float 1.41e-4, as '0.5' or as r'$\mathdefault{\frac{1}{2}}$' in float
0.5,
"""
# Can inline the Unicode escapes to the raw strings in Python 3.8+
match = re.match(
r"[^\d]*"
r"(?P<comp>1-)?"
r"(?P<comp>1" "[-\N{Minus Sign}]" r")?"
r"(?P<mant>\d*\.?\d*)?"
r"(?:\\cdot)?"
r"(?:10\^\{(?P<expo>-?\d*)})?"
r"(?:10\^\{(?P<expo>" "[-\N{Minus Sign}]" r"?\d*)})?"
r"[^\d]*$",
string,
)
if match:
comp = match["comp"] is not None
mantissa = float(match["mant"]) if match["mant"] else 1
expo = int(match["expo"]) if match["expo"] is not None else 0
expo = (int(match["expo"].replace("\N{Minus Sign}", "-"))
if match["expo"] is not None else 0)
value = mantissa * 10 ** expo
if match["mant"] or match["expo"] is not None:
if comp:
Expand Down Expand Up @@ -1044,8 +1047,8 @@ def test_use_overline(self):
Test the parameter use_overline
"""
x = 1 - 1e-2
fx1 = r"$\mathdefault{1-10^{-2}}$"
fx2 = r"$\mathdefault{\overline{10^{-2}}}$"
fx1 = "$\\mathdefault{1\N{Minus Sign}10^{\N{Minus Sign}2}}$"
fx2 = "$\\mathdefault{\\overline{10^{\N{Minus Sign}2}}}$"
form = mticker.LogitFormatter(use_overline=False)
assert form(x) == fx1
form.use_overline(True)
Expand Down
11 changes: 6 additions & 5 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ def __call__(self, x, pos=None):
vmin, vmax = self.axis.get_view_interval()
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=0.05)
s = self._num_to_string(x, vmin, vmax)
return s
return self.fix_minus(s)

def format_data(self, value):
with cbook._setattr_cm(self, labelOnlyBase=False):
Expand Down Expand Up @@ -1090,11 +1090,12 @@ def __call__(self, x, pos=None):
base = '%s' % b

if abs(fx) < min_exp:
return r'$\mathdefault{%s%g}$' % (sign_string, x)
s = r'$\mathdefault{%s%g}$' % (sign_string, x)
elif not is_x_decade:
return self._non_decade_format(sign_string, base, fx, usetex)
s = self._non_decade_format(sign_string, base, fx, usetex)
else:
return r'$\mathdefault{%s%s^{%d}}$' % (sign_string, base, fx)
s = r'$\mathdefault{%s%s^{%d}}$' % (sign_string, base, fx)
return self.fix_minus(s)


class LogFormatterSciNotation(LogFormatterMathtext):
Expand Down Expand Up @@ -1296,7 +1297,7 @@ def __call__(self, x, pos=None):
s = self._one_minus(self._format_value(1-x, 1-self.locs))
else:
s = self._format_value(x, self.locs, sci_notation=False)
return r"$\mathdefault{%s}$" % s
return r"$\mathdefault{%s}$" % self.fix_minus(s)

def format_data_short(self, value):
# docstring inherited
Expand Down