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

Skip to content

Commit 612cfae

Browse files
authored
Merge pull request #22140 from QuLogic/log-minus
Ensure log formatters use Unicode minus
2 parents ea04978 + 52003e4 commit 612cfae

File tree

7 files changed

+1228
-1195
lines changed

7 files changed

+1228
-1195
lines changed
Binary file not shown.

lib/matplotlib/tests/baseline_images/test_axes/errorbar_mixed.svg

Lines changed: 1208 additions & 1182 deletions
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,9 @@ def test_pyplot_axes():
25452545

25462546
@image_comparison(['log_scales'])
25472547
def test_log_scales():
2548+
# Remove this if regenerating the image.
2549+
plt.rcParams['axes.unicode_minus'] = False
2550+
25482551
fig, ax = plt.subplots()
25492552
ax.plot(np.log(np.linspace(0.1, 100)))
25502553
ax.set_yscale('log', base=5.5)

lib/matplotlib/tests/test_colorbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ def test_colorbar_format(fmt):
577577
im.set_norm(LogNorm(vmin=0.1, vmax=10))
578578
fig.canvas.draw()
579579
assert (cbar.ax.yaxis.get_ticklabels()[0].get_text() ==
580-
r'$\mathdefault{10^{-2}}$')
580+
'$\\mathdefault{10^{\N{Minus Sign}2}}$')
581581

582582

583583
def test_colorbar_scale_reset():

lib/matplotlib/tests/test_ticker.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ def test_basic(self, labelOnlyBase, base, exponent, locs, positions,
637637
formatter.axis = FakeAxis(1, base**exponent)
638638
vals = base**locs
639639
labels = [formatter(x, pos) for (x, pos) in zip(vals, positions)]
640+
expected = [label.replace('-', '\N{Minus Sign}') for label in expected]
640641
assert labels == expected
641642

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

667668

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

696697

697698
class TestLogFormatter:
@@ -907,19 +908,21 @@ def logit_deformatter(string):
907908
float 1.41e-4, as '0.5' or as r'$\mathdefault{\frac{1}{2}}$' in float
908909
0.5,
909910
"""
911+
# Can inline the Unicode escapes to the raw strings in Python 3.8+
910912
match = re.match(
911913
r"[^\d]*"
912-
r"(?P<comp>1-)?"
914+
r"(?P<comp>1" "[-\N{Minus Sign}]" r")?"
913915
r"(?P<mant>\d*\.?\d*)?"
914916
r"(?:\\cdot)?"
915-
r"(?:10\^\{(?P<expo>-?\d*)})?"
917+
r"(?:10\^\{(?P<expo>" "[-\N{Minus Sign}]" r"?\d*)})?"
916918
r"[^\d]*$",
917919
string,
918920
)
919921
if match:
920922
comp = match["comp"] is not None
921923
mantissa = float(match["mant"]) if match["mant"] else 1
922-
expo = int(match["expo"]) if match["expo"] is not None else 0
924+
expo = (int(match["expo"].replace("\N{Minus Sign}", "-"))
925+
if match["expo"] is not None else 0)
923926
value = mantissa * 10 ** expo
924927
if match["mant"] or match["expo"] is not None:
925928
if comp:
@@ -1044,8 +1047,8 @@ def test_use_overline(self):
10441047
Test the parameter use_overline
10451048
"""
10461049
x = 1 - 1e-2
1047-
fx1 = r"$\mathdefault{1-10^{-2}}$"
1048-
fx2 = r"$\mathdefault{\overline{10^{-2}}}$"
1050+
fx1 = "$\\mathdefault{1\N{Minus Sign}10^{\N{Minus Sign}2}}$"
1051+
fx2 = "$\\mathdefault{\\overline{10^{\N{Minus Sign}2}}}$"
10491052
form = mticker.LogitFormatter(use_overline=False)
10501053
assert form(x) == fx1
10511054
form.use_overline(True)

lib/matplotlib/ticker.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ def __call__(self, x, pos=None):
10001000
vmin, vmax = self.axis.get_view_interval()
10011001
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=0.05)
10021002
s = self._num_to_string(x, vmin, vmax)
1003-
return s
1003+
return self.fix_minus(s)
10041004

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

10921092
if abs(fx) < min_exp:
1093-
return r'$\mathdefault{%s%g}$' % (sign_string, x)
1093+
s = r'$\mathdefault{%s%g}$' % (sign_string, x)
10941094
elif not is_x_decade:
1095-
return self._non_decade_format(sign_string, base, fx, usetex)
1095+
s = self._non_decade_format(sign_string, base, fx, usetex)
10961096
else:
1097-
return r'$\mathdefault{%s%s^{%d}}$' % (sign_string, base, fx)
1097+
s = r'$\mathdefault{%s%s^{%d}}$' % (sign_string, base, fx)
1098+
return self.fix_minus(s)
10981099

10991100

11001101
class LogFormatterSciNotation(LogFormatterMathtext):
@@ -1296,7 +1297,7 @@ def __call__(self, x, pos=None):
12961297
s = self._one_minus(self._format_value(1-x, 1-self.locs))
12971298
else:
12981299
s = self._format_value(x, self.locs, sci_notation=False)
1299-
return r"$\mathdefault{%s}$" % s
1300+
return r"$\mathdefault{%s}$" % self.fix_minus(s)
13001301

13011302
def format_data_short(self, value):
13021303
# docstring inherited

0 commit comments

Comments
 (0)