gpt4 book ai didi

python - 是否有 matplotlib.ticker.LogFormatterSciNotation 的非数学版本?

转载 作者:bug小助手 更新时间:2023-10-28 10:51:08 31 4
gpt4 key购买 nike

我正在尝试使用 pgf_with_latex 绘制带有对数 y 轴的图形,即所有文本格式都由 pdflatex 完成。在我的 matplotlib rc 参数中,我定义了要使用的字体。我的问题来了:标准的 matplotlib.ticker.LogFormatterSciNotation 格式化程序使用了数学文本,因此使用了数学字体,它不适合其余字体(无衬线)。

如何使用 matplotlib.ticker 中的格式化程序格式化 y 轴标签,以便将标签格式化为 10 的幂和上标幂?更具体地说:如何让这些 yticklabels 以相同的方式格式化,但使用 xticklabels 中的字体?

我已经尝试过使用 matplotlib.ticker 提供的不同格式化程序,但它们都没有按照我想要的方式编写指数。

下面是我所说的 MWE 的示例。 example plot

import matplotlib as mpl

mpl.use('pgf')
pgf_with_latex = {
"pgf.texsystem": "pdflatex",
"font.family": "sans-serif",
"text.usetex": False,
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}",
r"\usepackage{tgheros}", # TeX Gyre Heros sans serif
r"\usepackage[T1]{fontenc}"
]
}

mpl.rcParams.update(pgf_with_latex)
import matplotlib.pyplot as plt

fig = plt.figure(figsize=[3, 2])
ax = fig.add_subplot(111)
ax.set_yscale("log")
ax.minorticks_off()
ax.set_xlabel("sans-serif font label")
ax.set_ylabel("math font label")
plt.gca().set_ylim([1, 10000])
plt.gcf().tight_layout()


plt.savefig('{}.pdf'.format("test"))

注意:必须在您的系统上安装 TeX 发行版才能运行它。我使用了 MikTex 2.9。还有 Python 3.6.2 和 matplotlib 2.1.2。

最佳答案

您可以继承 LogFormatterExponent 以使用 "10\textsuperscript{x}" 格式化刻度,其中 x 是指数。这不会使用数学模式 tex,即文本周围没有 $ 符号,因此将使用序言中指定的文本字体(在这种情况下是没有衬线的字体)。

import matplotlib as mpl
from matplotlib.ticker import LogFormatterExponent

mpl.use('pgf')
pgf_with_latex = {
"pgf.texsystem": "pdflatex",
"font.family": "sans-serif",
"text.usetex": False,
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}",
r"\usepackage{tgheros}", # TeX Gyre Heros sans serif
r"\usepackage[T1]{fontenc}"
]
}
mpl.rcParams.update(pgf_with_latex)
import matplotlib.pyplot as plt

class LogFormatterTexTextMode(LogFormatterExponent):
def __call__(self, x, pos=None):
x = LogFormatterExponent.__call__(self, x,pos)
s = r"10\textsuperscript{{{}}}".format(x)
return s

fig = plt.figure(figsize=[3, 2])
ax = fig.add_subplot(111)
ax.set_yscale("log")
ax.yaxis.set_major_formatter(LogFormatterTexTextMode())
ax.minorticks_off()
ax.set_xlabel("sans-serif font label")
ax.set_ylabel("text mode tex label")
plt.gca().set_ylim([0.01, 20000])
plt.gcf().tight_layout()


plt.savefig('{}.pdf'.format("test"))

enter image description here

关于python - 是否有 matplotlib.ticker.LogFormatterSciNotation 的非数学版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52277797/

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