Skip to content

Commit 62bac20

Browse files
Vashesh08timhoffm
authored andcommitted
Auto-escape % symbol in LaTeX in pie labels
Fixes Issue matplotlib#26377 - Adding Support For % operator in Latex Excaping The % Operator in LaTeX for the Pie method Update _axes.py Added usetex key in textprops dictionary in pie method Removing Flake Issues and Prioritizing textprops parameter Fixes % operator in Latex in Pie Method Removed Trailing Whitespace Removed Unnecessary Label Test in Pie Update lib/matplotlib/tests/test_axes.py Skip Test when latex is not available Co-authored-by: Kyle Sunden <[email protected]> Update lib/matplotlib/tests/test_axes.py Update test_name for % operator in Latex in Pie Method Co-authored-by: Kyle Sunden <[email protected]> Added Dependency for needs_usetex testing for checking Latex dependency Comment For Using Regex To Support % Symbol in Latex Comments added to escape % if not already escaped using regex Co-authored-by: Tim Hoffmann <[email protected]>
1 parent d0faf22 commit 62bac20

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Percent sign in pie labels auto-escaped with ``usetex=True``
2+
------------------------------------------------------------
3+
4+
It is common, with `.Axes.pie`, to specify labels that include a percent sign
5+
(``%``), which denotes a comment for LaTeX. When enabling LaTeX with
6+
:rc:`text.usetex` or passing ``textprops={"usetex": True}``, this would cause
7+
the percent sign to disappear.
8+
9+
Now, the percent sign is automatically escaped (by adding a preceding
10+
backslash) so that it appears regardless of the ``usetex`` setting. If you have
11+
pre-escaped the percent sign, this will be detected, and remain as is.

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import math
55
from numbers import Integral, Number, Real
66

7+
import re
78
import numpy as np
89
from numpy import ma
910

@@ -3377,6 +3378,9 @@ def get_next_color():
33773378
else:
33783379
raise TypeError(
33793380
'autopct must be callable or a format string')
3381+
if mpl._val_or_rc(textprops.get("usetex"), "text.usetex"):
3382+
# escape % (i.e. \%) if it is not already escaped
3383+
s = re.sub(r"([^\\])%", r"\1\\%", s)
33803384
t = self.text(xt, yt, s,
33813385
clip_on=False,
33823386
horizontalalignment='center',

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
assert_allclose, assert_array_equal, assert_array_almost_equal)
3939
from matplotlib.testing.decorators import (
4040
image_comparison, check_figures_equal, remove_ticks_and_titles)
41+
from matplotlib.testing._markers import needs_usetex
4142

4243
# Note: Some test cases are run twice: once normally and once with labeled data
4344
# These two must be defined in the same test function or need to have
@@ -9008,3 +9009,16 @@ def test_boxplot_tick_labels():
90089009
# Test the new tick_labels parameter
90099010
axs[1].boxplot(data, tick_labels=['A', 'B', 'C'])
90109011
assert [l.get_text() for l in axs[1].get_xticklabels()] == ['A', 'B', 'C']
9012+
9013+
9014+
@needs_usetex
9015+
@check_figures_equal()
9016+
def test_latex_pie_percent(fig_test, fig_ref):
9017+
9018+
data = [20, 10, 70]
9019+
9020+
ax = fig_test.subplots()
9021+
ax.pie(data, autopct="%1.0f%%", textprops={'usetex': True})
9022+
9023+
ax1 = fig_ref.subplots()
9024+
ax1.pie(data, autopct=r"%1.0f\%%", textprops={'usetex': True})

0 commit comments

Comments
 (0)