在 matplotlib.plt.text() 中使用时从 .dvi 裁剪的 tikzpicture
tikzpicture cropped from `.dvi` when used in matplotlib.plt.text()
免责声明:我完全理解在 rcparams 中使用 text.latex.preamble
不受 matplottib 官方支持。
注意:post编辑了同样的问题 on the matplotlib discourse "Community" page。我在这里重新 post 它,希望能达到更广泛的“LaTeX/tikz 观众”。
亲爱的蜂群思维,
我 need/want 使用 tikz
LaTeX package 在 matplotlib plt.text()
调用中绘制符号。一切顺利(从某种意义上说,我没有 LaTeX/matplotlib 错误)...除了实际上没有在图形上绘制符号外?!
这是一个 MWE:
from matplotlib import pyplot as plt
plt.style.use('./latex.mplstyle')
plt.close(1)
plt.figure(1, figsize=(4, 3))
plt.text(0.5, 0.7, r'This is regular text', ha='center', va='center')
plt.text(0.5, 0.5, r'$\rightarrow$ \begin{tikzpicture}\draw [thick] (0,0) circle [radius=2ex];\end{tikzpicture}', ha='center', va='center')
plt.savefig('test.png')
plt.show()
latex.mplstyle
的内容是:
text.usetex: True
text.latex.preamble: \usepackage{tikz}
输出如下所示:
问题:箭头右边应该有个圆
那么,matplotlib 与 pdflatex
相比,returns 下图有何不同:
处理以下(相同的)LaTeX 代码时:
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
$\rightarrow$ \begin{tikzpicture}\draw [thick] (0,0) circle [radius=2ex];\end{tikzpicture}
\end{document}
tikz
包肯定被正确加载,因为 \tikzpicture
命令在 编译 matplotlib 图时不会引发任何错误。
旁注 1:据我所知,相同的行为意味着 tikzsymbols
LaTeX 包中的符号不会出现在 plt.text()
调用中。
旁注 2:我显然需要绘制更多(但不是那么多)而不是带有 tikz
的圆...这只是 MWE!
编辑(经过一些挖掘感谢@samcarter_is_at_topanswers.xyz的建议)
事实证明,LaTeX 生成的 .dvi
文件看起来符合预期。然而,这些似乎在被 matplotlib 图摄取之前被裁剪了。无论做什么,这种裁剪似乎都没有意识到 tikzpicture
的存在。这是我在 .dvi
、屏幕上、.png
和来自 matplotlib 的 .pdf
中看到的图片:
所以问题变成:
什么机制负责将 LaTeX .dvi
插入 matplotlib 图中,为什么它不知道 \tikzpicture
的存在?
并且,可能:
我可以把 \tikzpicture
放在某种 LaTeX 边界框内,让它对 matplotlib 可见吗?
请参阅 this Matplotlib's discourse question 以获得导致以下答案的完整线程
原因:
Matplotlib 需要诸如基线、上升和下降之类的字体指标来包含文本。 \tikzpicture
没有提供任何内容,因此在 dvi 的裁剪中不考虑。
Solution/work-around:
使用pgf
后端将图形导出为pdf
and/orpng
格式,即:plt.savefig(..., backend='pgf')
此方法还可以成功处理包含 tiksymbols
LaTeX 包中的项目。但是,它不允许在屏幕上显示这些元素。
免责声明:我完全理解在 rcparams 中使用 text.latex.preamble
不受 matplottib 官方支持。
注意:post编辑了同样的问题 on the matplotlib discourse "Community" page。我在这里重新 post 它,希望能达到更广泛的“LaTeX/tikz 观众”。
亲爱的蜂群思维,
我 need/want 使用 tikz
LaTeX package 在 matplotlib plt.text()
调用中绘制符号。一切顺利(从某种意义上说,我没有 LaTeX/matplotlib 错误)...除了实际上没有在图形上绘制符号外?!
这是一个 MWE:
from matplotlib import pyplot as plt
plt.style.use('./latex.mplstyle')
plt.close(1)
plt.figure(1, figsize=(4, 3))
plt.text(0.5, 0.7, r'This is regular text', ha='center', va='center')
plt.text(0.5, 0.5, r'$\rightarrow$ \begin{tikzpicture}\draw [thick] (0,0) circle [radius=2ex];\end{tikzpicture}', ha='center', va='center')
plt.savefig('test.png')
plt.show()
latex.mplstyle
的内容是:
text.usetex: True
text.latex.preamble: \usepackage{tikz}
输出如下所示:
问题:箭头右边应该有个圆
那么,matplotlib 与 pdflatex
相比,returns 下图有何不同:
处理以下(相同的)LaTeX 代码时:
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
$\rightarrow$ \begin{tikzpicture}\draw [thick] (0,0) circle [radius=2ex];\end{tikzpicture}
\end{document}
tikz
包肯定被正确加载,因为 \tikzpicture
命令在 编译 matplotlib 图时不会引发任何错误。
旁注 1:据我所知,相同的行为意味着 tikzsymbols
LaTeX 包中的符号不会出现在 plt.text()
调用中。
旁注 2:我显然需要绘制更多(但不是那么多)而不是带有 tikz
的圆...这只是 MWE!
编辑(经过一些挖掘感谢@samcarter_is_at_topanswers.xyz的建议)
事实证明,LaTeX 生成的 .dvi
文件看起来符合预期。然而,这些似乎在被 matplotlib 图摄取之前被裁剪了。无论做什么,这种裁剪似乎都没有意识到 tikzpicture
的存在。这是我在 .dvi
、屏幕上、.png
和来自 matplotlib 的 .pdf
中看到的图片:
所以问题变成:
什么机制负责将 LaTeX .dvi
插入 matplotlib 图中,为什么它不知道 \tikzpicture
的存在?
并且,可能:
我可以把 \tikzpicture
放在某种 LaTeX 边界框内,让它对 matplotlib 可见吗?
请参阅 this Matplotlib's discourse question 以获得导致以下答案的完整线程
原因:
Matplotlib 需要诸如基线、上升和下降之类的字体指标来包含文本。 \tikzpicture
没有提供任何内容,因此在 dvi 的裁剪中不考虑。
Solution/work-around:
使用pgf
后端将图形导出为pdf
and/orpng
格式,即:plt.savefig(..., backend='pgf')
此方法还可以成功处理包含 tiksymbols
LaTeX 包中的项目。但是,它不允许在屏幕上显示这些元素。