Labplale Matplotlib Label Labels, Minus Sign Too Long in Latex Font

I am using 'text.usetex': True in matplotib. This is good for linear scale scenes. However, for the y-ticks log scale, it looks like this:

Minus signs in exhibitors occupy a lot of horizontal space in the plot, which is not very pleasant. I want it to look like this:

This is one of gnuplot and it does not use tex font. I would like to use matplotlib if it displayed in tex, but the minus signs in 10 ^ {- n} should be shorter. Is it possible?

0
source share
2 answers

Dietrich gave you a good answer, but if you want to keep all the functionality (not base 10, non-integer metrics) of LogFormatter , then you can create your own formatter:

 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 

The only thing that really does is to capture the output of conventional formatting, find possible negative exponents, and change the LaTeX code of the mathematical minus to something else. Of course, if you come up with some kind of creative LaTex with \scalebox or something equivalent, you can do it.

It:

 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") 

creates:

enter image description here

It’s good that this decision is that it changes the result as little as possible.

+2
source

The length of the minus sign is the solution to your LaTeX font - in mathematical mode, binary and unary minuses have the same length. According to this answer you can create your own shortcuts. Try the following:

 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() 

To obtain: Resulting plot

+3
source

Source: https://habr.com/ru/post/1239175/


All Articles