Matplotlib with pgf backend, problem with underscore

  • Thread starter fluidistic
  • Start date
  • Tags
    Matplotlib
In summary, I am getting an error when trying to reference an equation in a latex document. The error is caused by the presence of an underscore in the equation's \label{} value. If the equation's \label{} contains no underscore, then the file.pgf is created and can be correctly imported into the latex document.
  • #1
fluidistic
Gold Member
3,926
261
I use matplotlib to create files.pgf that I can \include{} in my latex documents. All works nicely, except when I want to make a reference to some equation of my latex document. For example, I when plot several curves on a graph and label them as "Coming from expression ..." where ... is \ref{eq:some_equation}, then I get an error and no file.pgf is produced. If, however, the \label{} of the equation contains no underscore, then there is no problem, the file.pgf is created and can be correctly imported into the latex document.

How can I make matplotlib able to create the file.pgf without removing the underscores? Because then I also have to remove it in all my latex files referencing to that particular equation.

I already tried \_ instead of _, to no avail.
Here might be a MWE (untested):
Python:
import matplotlib as mpl
mpl.use('pgf')
import matplotlib.pyplot as plt
x, y = [1], [2]
plt.plot(x, y, label='coming from expr.\\ref{eq:heat_eq}')
plt.savefig('file.pgf')
plt.close()

This MWE should return an error, unless the underscore in heat_eq is removed. Ideally, I want to make matplotlib understand that it can deal with the underscore.
 
Physics news on Phys.org
  • #3
jedishrfu said:
From matplotlib:

https://matplotlib.org/tutorials/text/mathtext.html

The underscore is a special character in latex to indicate subscripts.

Some folks have suggested using \ to escape the underscore as in \_
As I wrote in my post, I already tried that suggestion. I also tried to put an r before the string containing this problematic part, to no avail.
 
  • #4
Did you do both? The r means the string is a raw unprocessed string so the \ will not be removed.

In C code, we would have to routinely use \\ because processing would remove one leaving \ so that's another thing you could try without the r.

Code:
Sometimes meta chars can be mega headaches. In one case, I had to
use \\\\ just to get one \ because of double processing of the string
ie \\\\ to \\ to \

(and for PF use [ code ] tags because \ was processed here too 
without them.
 
  • #5
Yeah, I tried every combinations possible:
_\
_

And the same thing with an r in front of the 'problematic_part'. Like that: label=r'coming from expr.\\ref{eq:heat_eq}' and label=r'coming from expr.\\ref{eq:heat\_eq}'

Nothing worked. What I eventually did was to remove the underscore and do the same in my whole .tex files (about 10 occurrences). That worked, of course, but this is not a good way to deal with the problem since it is bypassing the problem rather than tackling it.
 
  • #6
The alternative is to give your figure a caption which tells the reader what expression is being plotted (either instead of, or in addition to, including a legend in the figure which doesn't refer to expressions in the particular document - that way you can re-use the figure elsewhere.)

Code:
\begin{figure}
\begin{graphics}
\end{graphics}
\caprion{Plot of expression \ref{...} (solid lines) and ...}
\end{figure}

This is my usual practice, since I generally produce PNG output from matplotlib.
 
  • Like
Likes fluidistic
  • #7
pasmith said:
The alternative is to give your figure a caption which tells the reader what expression is being plotted (either instead of, or in addition to, including a legend in the figure which doesn't refer to expressions in the particular document - that way you can re-use the figure elsewhere.)

Code:
\begin{figure}
\begin{graphics}
\end{graphics}
\caprion{Plot of expression \ref{...} (solid lines) and ...}
\end{figure}

This is my usual practice, since I generally produce PNG output from matplotlib.
I see, yeah that could also be another solution to bypass the problem, although in that case the end result is not what I had in mind.

Interesting, but I do not really understand your last sentence. Why would it matter that you generally produce png output rather than say, pgf or pdf?
 
  • #8
fluidistic said:
Interesting, but I do not really understand your last sentence. Why would it matter that you generally produce png output rather than say, pgf or pdf?

LaTeX won't find a \ref command in a compressed list of "this pixel is this color" data.
 
  • Like
Likes fluidistic

Related to Matplotlib with pgf backend, problem with underscore

1. Why am I getting an error when trying to use underscores in my matplotlib plot with pgf backend?

This is a common issue when using the pgf backend in matplotlib. The reason for this is that the pgf backend uses LaTeX for typesetting, and underscores have a special meaning in LaTeX. To avoid this error, you can either use a different backend or escape the underscores in your code.

2. How can I escape underscores in my code when using the pgf backend in matplotlib?

To escape underscores in your code, you can use the backslash (\) character before the underscore. For example, if you want to plot the function y = x^2, you would write it as y = x\^2 in your code. This will ensure that the underscores are not interpreted as special characters by LaTeX.

3. Can I use the pgf backend in matplotlib with underscores if I am using Python 3?

Yes, you can still use the pgf backend in matplotlib with underscores if you are using Python 3. However, you may need to make some adjustments to your code as Python 3 has stricter rules for handling underscores in variable names. It is recommended to use the backslash (\) character to escape underscores in your code to avoid any errors.

4. Is there a way to change the default behavior of the pgf backend in matplotlib to allow underscores?

Yes, you can change the default behavior of the pgf backend in matplotlib to allow underscores by setting the text.usetex option to False in your code. This will disable the use of LaTeX for typesetting and allow you to use underscores without any issues. However, keep in mind that this may affect the formatting and quality of your plot.

5. Are there any alternative solutions for using underscores in matplotlib with the pgf backend?

Yes, there are a few alternative solutions for using underscores in matplotlib with the pgf backend. One option is to use the raw string format in your code, which will treat underscores as regular characters. Another option is to use the pgfutils package, which provides functions for handling special characters in pgf plots, including underscores.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
4K
Replies
2
Views
646
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
5K
  • Advanced Physics Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
21
Views
4K
Back
Top