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

Skip to content

Commit 00c8a1d

Browse files
committed
Correctly format floats passed to pgf backend.
Using scientific notation (implicitly via "{}".format) results in e.g. `use("pgf"); figtext(1e-6, 1e-6, "foo"); savefig("/tmp/test.pdf")` lead to tex failing with `! Package PGF Math Error: Unknown operator `i' or `in' (in '6.4e-06in').`
1 parent 84e531b commit 00c8a1d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
_log = logging.getLogger(__name__)
2626

2727

28-
###############################################################################
28+
# Note: When formatting floating point values, it is important to use the
29+
# %f/{:f} format rather than %s/{} to avoid triggering scientific notation,
30+
# which is not recognized by TeX.
2931

3032

3133
def get_fontspec():
@@ -724,14 +726,14 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
724726
valign = {"top": "top", "bottom": "bottom",
725727
"baseline": "base", "center": ""}
726728
text_args.extend([
727-
f"x={x/dpi}in",
728-
f"y={y/dpi}in",
729+
f"x={x/dpi:f}in",
730+
f"y={y/dpi:f}in",
729731
halign[mtext.get_horizontalalignment()],
730732
valign[mtext.get_verticalalignment()],
731733
])
732734
else:
733735
# if not, use the text layout provided by Matplotlib.
734-
text_args.append(f"x={x/dpi}in, y={y/dpi}in, left, base")
736+
text_args.append(f"x={x/dpi:f}in, y={y/dpi:f}in, left, base")
735737

736738
if angle != 0:
737739
text_args.append("rotate=%f" % angle)

0 commit comments

Comments
 (0)