Matplotlib 对数刻度刻度标签,乳胶字体中的减号太长

tho*_*rmi 3 python latex matplotlib

我在 matplotib 中使用 'text.usetex': True 。这对于具有线性比例的图来说非常有用。然而,对于对数刻度,y 刻度如下所示:

指数中的负号占用了绘图中的大量水平空间,这不太好。我希望它看起来像这样:

该字体来自 gnuplot,并且没有使用 tex-font。我想使用 matplotlib,将其呈现在 tex 中,但 10^{-n} 中的减号应该更短。那可能吗?

Die*_*ich 5

减号的长度由你的 LaTeX 字体决定 - 在数学模式下,二进制和一元减号具有相同的长度。根据这个答案你可以制作自己的标签。尝试这个:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import ticker


mpl.rcParams['text.usetex']=True
mpl.rcParams['text.latex.unicode']=True

def my_formatter_fun(x, p):
    """ Own formatting function """
    return r"$10$\textsuperscript{%i}" % np.log10(x)  #  raw string to avoid "\\"


x = np.linspace(1e-6,1,1000)
y = x**2

fg = plt.figure(1); fg.clf()
ax = fg.add_subplot(1, 1, 1)
ax.semilogx(x, x**2)
ax.set_title("$10^{-3}$ versus $10$\\textsuperscript{-3} versus "
             "10\\textsuperscript{-3}")
# Use own formatter:
ax.get_xaxis().set_major_formatter(ticker.FuncFormatter(my_formatter_fun))

fg.canvas.draw()
plt.show()
Run Code Online (Sandbox Code Playgroud)

获得:结果图