I am having the problem where a pgf created using matplotlib.pyplot outputs hyphens instead of minus signs, which Latex cannot interpret.
I attempted to use the solution found here, but it changes numbers from integers to floats (i.e. 2000 becomes 2000.0) in the tick labels. I am looking for a solution that fixes the signs but keeps the default formatting in pyplot otherwise.
Any ideas? Example below.
myplot.py
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
def math_formatter(x, pos):
return "$%s$" % x
plt.figure()
plt.plot([1,2,3,4,5,6,7],[-1,-2,3,4,5,6,7])
axis = plt.gca()
axis.xaxis.set_major_formatter(FuncFormatter(math_formatter))
axis.yaxis.set_major_formatter(FuncFormatter(math_formatter))
plt.show()
mylatex.tex
\documentclass{article}
\usepackage{pgf}
\begin{document}
\begin{figure}[H]
\centering
\input{myplot.pgf}
\end{figure}
\end{document}
If you plot without the formatter argument, you get the standard formatting as int, but latex will not recognize the hyphens as minus signs. If you use the formatter argument, all int will become floats.
I am looking for a solution where the hyphens are changed to minus signs but no matter the argument (int or float or otherwise) the ticks will behave like the default behavior of pyplot (except the hyphens are minus signs).
The matplotlib. pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.
Matplotlib can use LaTeX to render text. This is activated by setting text. usetex : True in your rcParams, or by setting the usetex property to True on individual Text objects.
The dpi method of figure module of matplotlib library is the resolution in dots per inch. Syntax: fig.dpi. Parameters: This method does not accept any parameters. Returns: This method returns resolution in dots per inch.
pad: This parameter is used for padding between the figure edge and the edges of subplots, as a fraction of the font size. h_pad, w_pad: These parameter are used for padding (height/width) between edges of adjacent subplots, as a fraction of the font size.
Recent versions of Matplotlib use, by default, the typographically "correct" (this is debatable) unicode minus signs (U+2212) to express negative numbers, and not the ASCII hyphens. On my system, the ASCII hyphen is interpreted without any problem in Latex, but the unicode minus sign is not by default.
Using a FuncFormatter
with the expression return '%i' % x
as you proposed here converts the minus sign to a hyphen and is a valid solution for Latex compatibility. In addition to this solution, below are two additional alternative solutions that could be used to solve this problem with a more "system-wide" approach.
matplotlib : use hyphen instead of minus sign
The ASCII hyphen (which is interpreted correctly by Latex by default) can be used instead of the unicode minus sign to express negative numbers in matplotlib. As per the documentation (http://matplotlib.org/1.3.0/examples/api/unicode_minus.html), it is possible to do this by doing:
matplotlib.rcParams['axes.unicode_minus'] = False
Latex : use unicode minus sign
If you prefer to use the minus sign instead of the hyphen, you can add this in the preamble of your Latex document:
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2212}{$-$}
Latex should then recognize the character U+2212
and express correctly the negative numbers with a minus sign. Below is the output I get with Latex with hypens (top) and with minus signs (bottom):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With