Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib日志刻度记号标签,latex字体中的减号太长_Python_Matplotlib_Latex - Fatal编程技术网

Python Matplotlib日志刻度记号标签,latex字体中的减号太长

Python Matplotlib日志刻度记号标签,latex字体中的减号太长,python,matplotlib,latex,Python,Matplotlib,Latex,我使用的是“text.usetex”:在matplotib中为True。这对于具有线性比例的绘图很好。但是,对于对数刻度,y刻度如下所示: 指数中的负号占据了绘图中大量的水平空间,这不是很好。我希望它看起来像这样: 这是gnuplot的,它没有使用tex字体。我想使用matplotlib,用tex表示,但是10^{-n}中的减号应该更短。这可能吗?减号的长度由LaTeX字体决定-在数学模式下,二进制和一元负号的长度相同。根据需要,您可以制作自己的标签。试试这个: import numpy a

我使用的是“text.usetex”:在matplotib中为True。这对于具有线性比例的绘图很好。但是,对于对数刻度,y刻度如下所示:

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


这是gnuplot的,它没有使用tex字体。我想使用matplotlib,用tex表示,但是10^{-n}中的减号应该更短。这可能吗?

减号的长度由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()

获取:

减号的长度由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()

要获得:

Dietrich
给了您一个很好的答案,但是如果您想保留
LogFormatter
的所有功能(非基数10,非整数指数),那么您可以创建自己的格式化程序:

import matplotlib.ticker
import matplotlib
import re

# create a definition for the short hyphen
matplotlib.rcParams["text.latex.preamble"].append(r'\mathchardef\mhyphen="2D')

class MyLogFormatter(matplotlib.ticker.LogFormatterMathtext):
    def __call__(self, x, pos=None):
        # call the original LogFormatter
        rv = matplotlib.ticker.LogFormatterMathtext.__call__(self, x, pos)

        # check if we really use TeX
        if matplotlib.rcParams["text.usetex"]:
            # if we have the string ^{- there is a negative exponent
            # where the minus sign is replaced by the short hyphen
            rv = re.sub(r'\^\{-', r'^{\mhyphen', rv)

        return rv
它真正做的唯一一件事是获取常用格式化程序的输出,找到可能的负指数,并将数学负号的LaTeX代码更改为其他代码。当然,如果你用
\scalebox
或类似的东西发明了一些创造性的乳胶,你可以这样做

这:

创建:


此解决方案的好处在于它尽可能少地更改输出。

Dietrich
给了您一个很好的答案,但是如果您想保留
LogFormatter
的所有功能(非基数10,非整数指数),那么您可以创建自己的格式化程序:

import matplotlib.ticker
import matplotlib
import re

# create a definition for the short hyphen
matplotlib.rcParams["text.latex.preamble"].append(r'\mathchardef\mhyphen="2D')

class MyLogFormatter(matplotlib.ticker.LogFormatterMathtext):
    def __call__(self, x, pos=None):
        # call the original LogFormatter
        rv = matplotlib.ticker.LogFormatterMathtext.__call__(self, x, pos)

        # check if we really use TeX
        if matplotlib.rcParams["text.usetex"]:
            # if we have the string ^{- there is a negative exponent
            # where the minus sign is replaced by the short hyphen
            rv = re.sub(r'\^\{-', r'^{\mhyphen', rv)

        return rv
它真正做的唯一一件事是获取常用格式化程序的输出,找到可能的负指数,并将数学负号的LaTeX代码更改为其他代码。当然,如果你用
\scalebox
或类似的东西发明了一些创造性的乳胶,你可以这样做

这:

创建:

此解决方案的好处在于它尽可能少地更改输出。

非常感谢!我最喜欢“10\\textsuperscript{-3}”版本,从现在起我将使用您的好功能!谢谢!我最喜欢“10\\textsuperscript{-3}”版本,从现在起我将使用您的好功能!