Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Hyphen or Minus Sign in Matplotlib versus Compatibility with Latex

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

like image 431
The Guy Avatar asked May 12 '15 21:05

The Guy


People also ask

What is the difference between matplotlib Pyplot and matplotlib?

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.

Does matplotlib use LaTeX?

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.

What does DPI mean in matplotlib?

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.

What is padding in matplotlib?

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.


1 Answers

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

enter image description here

like image 84
Jean-Sébastien Avatar answered Sep 20 '22 21:09

Jean-Sébastien