Skip to content

Commit 81507b7

Browse files
author
Gorshanov Vadim
committed
Improve the texification of axis label
This uses the siunitx and some of the common possible measurement units.
1 parent 60a3668 commit 81507b7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/tikzplotlib/_axes.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ def _common_texification(string):
1212
# Work around <https://github.com/matplotlib/matplotlib/issues/15493>
1313
return mpl_common_texification(string).replace("&", "\\&")
1414

15+
def _siunitx_texification(string: str) -> str:
16+
string = re.sub(r"\smm", r" \\si{\\mm}", string)
17+
string = re.sub(r"\s°C", r" \\si{\\celsius}", string)
18+
string = re.sub(r"\sA/s", r" \\si{\\angstrom\\per\\second}", string)
19+
string = re.sub(r"\sg/s", r" \\si{\\gram\\per\\second}", string)
20+
string = re.sub(r"\shour", r" \\si{\\hour}", string)
21+
string = re.sub(r"\scc", r" \\si{\\cc}", string)
22+
string = re.sub(r"\s\\%", r" \\si{\\percent}", string)
23+
return string
24+
1525

1626
class Axes:
1727
def __init__(self, data, obj): # noqa: C901
@@ -50,9 +60,9 @@ def __init__(self, data, obj): # noqa: C901
5060
xlabel = obj.get_xlabel()
5161
if xlabel:
5262
xlabel = _common_texification(xlabel)
63+
xlabel = _siunitx_texification(xlabel)
5364

5465
labelcolor = obj.xaxis.label.get_c()
55-
xlabel = re.sub(r"\smm", r" \\si{\\mm}", xlabel)
5666
xlabel_spl = xlabel.split(",")
5767
if len(xlabel_spl) == 2:
5868
xlabel = ",".join(["$" + xlabel_spl[0].replace(" ", "\\ ") + "$",
@@ -71,10 +81,13 @@ def __init__(self, data, obj): # noqa: C901
7181
ylabel = obj.get_ylabel()
7282
if ylabel:
7383
ylabel = _common_texification(ylabel)
84+
ylabel = _siunitx_texification(ylabel)
7485

7586
ylabel_spl = ylabel.split(",")
7687
if len(ylabel_spl) == 2:
77-
ylabel = ",".join(["$" + ylabel_spl[0].replace(" ", "\\ ") + "$",
88+
ylabel = ",".join(["$" + ylabel_spl[0].replace(" ",
89+
"\\ ").replace("+-", r"\pm").replace("-",
90+
r"\mhyphen ") + "$",
7891
ylabel_spl[1]])
7992

8093
labelcolor = obj.yaxis.label.get_c()

0 commit comments

Comments
 (0)