是否存在matplotlib.ticker.LogFormatterSciNotation的非数学版本?

Ian*_*Ian 7 python formatting plot latex matplotlib

我试图使用对数y轴绘制图形pgf_with_latex,即所有文本格式都由pdflatex完成.在我的matplotlib rc参数中,我定义了一个要使用的字体.这是我的问题:标准matplotlib.ticker.LogFormatterSciNotation格式化程序使用数学文本,因此使用数学字体,它不适合其他字体(sans-serif).

如何使用格式化程序格式化y轴标签,matplotlib.ticker以便我获得标签格式为10的权力,并带有上标权限?更具体一点:我如何使用xticklabels中的字体以相同的方式格式化这些yticklabels?

我已经尝试过使用不同的格式化程序matplotlib.ticker,但是没有一个像我想要的那样编​​写指数.

以下是我对MWE的意思. 示例情节

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"))
Run Code Online (Sandbox Code Playgroud)

警告:必须在系统上安装TeX分发才能运行此分发.我使用了MikTex 2.9.还有Python 3.6.2和matplotlib 2.1.2.

Imp*_*est 2

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

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"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述