Skip to content

Commit f0657aa

Browse files
committed
fix log formatting for y axis
1 parent 9f02583 commit f0657aa

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/matplotgl/axes.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .line import Line
1212
from .mesh import Mesh
1313
from .points import Points
14-
from .utils import latex_to_html
14+
from .utils import html_to_svg, latex_to_html
1515
from .widgets import ClickableHTML
1616

1717

@@ -147,7 +147,7 @@ def __init__(self, *, ax: MplAxes, figure=None) -> None:
147147
"grid_area": "cursor",
148148
"padding": "0",
149149
"margin": "0",
150-
"width": "80px",
150+
"width": "6em",
151151
},
152152
)
153153

@@ -353,7 +353,9 @@ def _make_xticks(self):
353353

354354
bottom_string = (
355355
f'<svg height="calc(1.2em + {tick_length}px + {label_offset}px)" '
356-
f'width="{self.width}"><line x1="0" y1="0" x2="{self.width}" y2="0" '
356+
f'width="{self.width}"><line x1="0" y1="0" '
357+
# f'width="calc(0.5em + {self.width}px)"><line x1="0" y1="0" '
358+
f'x2="{self.width}" y2="0" '
357359
f'style="stroke:black;stroke-width:{self._spine_linewidth}" />'
358360
)
359361

@@ -375,7 +377,7 @@ def _make_xticks(self):
375377
bottom_string += (
376378
f'<text x="{x}" y="{tick_length + label_offset}" '
377379
'text-anchor="middle" dominant-baseline="hanging">'
378-
f"{latex_to_html(label)}</text>"
380+
f"{html_to_svg(latex_to_html(label), baseline='hanging')}</text>"
379381
)
380382

381383
bottom_string += "</svg></div>"
@@ -431,7 +433,7 @@ def _make_yticks(self):
431433
left_string += (
432434
f'<text x="{width2}" '
433435
f'y="{y}" text-anchor="end" dominant-baseline="middle">'
434-
f"{latex_to_html(label)}</text>"
436+
f"{html_to_svg(latex_to_html(label), baseline='middle')}</text>"
435437
)
436438

437439
left_string += "</svg></div>"

src/matplotgl/utils.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ def latex_to_html(latex_str: str) -> str:
147147
# Special cases that don't follow the pattern (optional overrides)
148148
special_replacements = {
149149
"&cdot;": "&middot;",
150-
"<sup>": '<tspan dy="-7%" font-size="10">',
151-
"</sup>": "</tspan>",
152-
"<sub>": '<tspan dy="7%" font-size="10">',
153-
"</sub>": "</tspan>",
150+
# "<sup>": '<tspan dy="-7%" font-size="70%">',
151+
# "</sup>": "</tspan>",
152+
# "<sub>": '<tspan dy="7%" font-size="10">',
153+
# "</sub>": "</tspan>",
154154
}
155155

156156
for entity, replacement in special_replacements.items():
@@ -159,6 +159,42 @@ def latex_to_html(latex_str: str) -> str:
159159
return s
160160

161161

162+
def html_to_svg(text: str, baseline: str) -> str:
163+
"""Convert HTML text to SVG-compatible text using tspan for subscripts/superscripts.
164+
165+
Parameters
166+
----------
167+
text:
168+
The input HTML text.
169+
baseline:
170+
The dominant baseline alignment for the text ('hanging', 'middle', 'baseline').
171+
172+
Returns
173+
-------
174+
str
175+
The SVG-compatible text.
176+
"""
177+
replacements = {
178+
"hanging": {
179+
"<sup>": '<tspan dy="-7%" font-size="70%">',
180+
"</sup>": "</tspan>",
181+
"<sub>": '<tspan dy="7%" font-size="70%">',
182+
"</sub>": "</tspan>",
183+
},
184+
"middle": {
185+
"<sup>": '<tspan dy="-2%" font-size="70%">',
186+
"</sup>": "</tspan>",
187+
"<sub>": '<tspan dy="2%" font-size="70%">',
188+
"</sub>": "</tspan>",
189+
},
190+
}
191+
192+
for entity, replacement in replacements[baseline].items():
193+
text = text.replace(entity, replacement)
194+
195+
return text
196+
197+
162198
# def html_tags_to_svg(text: str) -> str:
163199
# """Convert <sup> and <sub> HTML tags to SVG superscript/subscript using tspan."""
164200

0 commit comments

Comments
 (0)