Estoy usando ‘text.usetex’: Verdadero en matplotib. Esto es bueno para plots con una escala lineal. Sin embargo, para una escala de registro, las marcas-y se ven así:
Los signos menos en los exponentes ocupan mucho espacio horizontal en una ttwig, lo que no es muy agradable. Quiero que se vea así:
Ese es de gnuplot, y no está usando la fuente tex. Me gustaría usar matplotlib, tenerlo renderizado en tex, pero los signos menos en 10 ^ {- n} deberían ser más cortos. ¿Es eso posible?
Dietrich
le dio una buena respuesta, pero si desea mantener toda la funcionalidad (sin base 10, exponentes no enteros) de LogFormatter
, puede crear su propio formateador:
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
Lo único que esto realmente hace es agarrar la salida del formateador habitual, encontrar los posibles exponentes negativos y cambiar el código LaTeX del matemático menos en otra cosa. Por supuesto, si inventas un LaTex creativo con \scalebox
o algo equivalente, puedes hacerlo.
Esta:
import matplotlib.pyplot as plt import numpy as np matplotlib.rcParams["text.usetex"] = True fig = plt.figure() ax = fig.add_subplot(111) ax.semilogy(np.linspace(0,5,200), np.exp(np.linspace(-2,3,200)*np.log(10))) ax.yaxis.set_major_formatter(MyLogFormatter()) fig.savefig("/tmp/shorthyphen.png")
crea:
Lo bueno de esta solución es que cambia la salida lo menos posible.
La longitud del signo menos es una decisión de su fuente LaTeX: en el modo matemático, los binarios y los menos únicos tienen la misma longitud. Según esta respuesta puedes hacer tus propias tags. Prueba esto:
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()
para obtener: