gpt4 book ai didi

python - 如何在 matplotlib 中获得更小的 latex 括号?

转载 作者:行者123 更新时间:2023-12-01 05:02:48 28 4
gpt4 key购买 nike

我在 matplotlib 中有以下表达式,使用 latex 代码:

ax.set_xlabel(r'\langle \epsilon \rangle', fontsize=18)

渲染时,epsilon 明显小于括号。括号的大小可以“包裹”一个大写字母。如何让括号变小?

最佳答案

在 LaTeX 的数学模式中,可以使用以下方法使事情变得更小:

  • \scriptstyle
  • \scriptscriptstyle

但是这些表达式在默认的 matplotlib 后端中不起作用:

#!/usr/bin/python3

import matplotlib
custom_preamble = {
"text.usetex": True,
}

from matplotlib import pyplot as plt


x = range(5)
y = range(5)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.text(0.5, 0.5,
r"${\scriptscriptstyle(} {\scriptstyle(} (\varepsilon$",
horizontalalignment = 'center',
backgroundcolor = "white",
verticalalignment = 'center',
transform = ax.transAxes,
)

ax.plot(x, y)

fig.savefig("mwe-1.png")

结果:

enter image description here

然而,为了更好地支持 LaTeX,可以使用 pgf 后端,然后它就可以工作了:

import matplotlib
matplotlib.use("pgf")
# the rest is the same as in the code above

结果:

enter image description here

pgf 后端已添加到 matplotlib 1.3。我认为您需要在系统上安装一个可用的 XeTeX(我认为相关的二进制文件称为 xelatex)。两个主要的 LaTeX 发行版(MiKTeX 和 TeXLive)都允许安装 XeTeX。

关于python - 如何在 matplotlib 中获得更小的 latex 括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25701916/

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