gpt4 book ai didi

python - text.usetex : True in matplotlib 有什么好处

转载 作者:太空狗 更新时间:2023-10-29 21:46:34 26 4
gpt4 key购买 nike

我打算写一篇论文,并开始设置一个标准的 Matplotlib 文件来控制绘图格式。但是,我在使用 text.usetex : True 选项时遇到了问题。尤其令人恼火的是,当我所有的数字都应该是无衬线字体时,刻度标签默认为衬线字体。确实 - 我在 rcParams 文件中将 font.family 设置为 sans-serif 但仍然看到问题,如 github here 中所标识.

此外,当我打开或关闭 usetex 时,其他文本看起来会有所不同 - 这似乎令人惊讶,因为我告诉 matplotlib 每次都使用相同的字体。

因此,我想知道使用 LaTeX 渲染的实际好处是什么?由于 Matplotlib 已经可以处理标签中的 LaTeX 命令,例如 xlabel('\alpha') 并且可以接受用户输入到 rcparams 文件中使用的字体,那么在文本上使用 LaTeX 有什么不同?

为了实现一致的 sans-serif 字体的目标,我可以不将 matplotlib rcparams 文件中的 font.sans-serif 设置为我在 LaTeX 中设置为 sans-serif 字体的字体吗?

感谢任何建议或提示!

最佳答案

当您需要 matplotlib 的内置数学文本中不存在的 LaTeX 功能时,usetex 设置特别有用。但它也提供了更好的排版,您不必担心数学文本的非标准部分。

如果比较下面的两个示例(基于 this page 末尾的示例),您会发现 LaTeX 版本在数学方面做得更好,尤其是求和。此外,mathtext 不知道 \displaystyle 但会自动使用该布局样式,这在某些情况下可能是不可取的。

关于刻度标签字体的问题,我相信 matplotlib 正在使用默认的 LaTeX 数学字体作为标签。如果您按照我在第二个示例中注释掉的代码行进行尝试,您应该能够得到您想要的。

如果你只是制作相对简单的图,你应该看看tikzplotlib .它允许您以 tikz 格式保存图形,以便轻松调整大小。请参阅我对 this 的回答有关详细信息的问题。


数学文本版本

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)

plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
plt.savefig('fig_mathtext.pdf')

enter image description here

LaTeX 版本

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
#rc('text.latex', preamble=r'\usepackage[eulergreek]{sansmath}\sansmath')

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)

figure()

plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\displaystyle\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
plt.savefig('fig_latex.pdf')

enter image description here

关于python - text.usetex : True in matplotlib 有什么好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21345922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com