简体   繁体   中英

How to correctly write the '<' and '>' symbols using Latex in Matplotlib

I was trying to have a plot using Matplotlib with a legend where I wanted to specify the range of some variables used to get the data I'm plotting.

So I was doing the following:

plt.rc('text', usetex=True)

plt.rc('font', family='serif')

plt.plot(E,P,"k*", label=r'with $\\vert \\Delta E \\vert$' + '< 0.039 && Mbc > 5.275') plt.plot(Eit,Pit, 'k.', label=r'after iteration: $\\vert \\Delta E \\vert$ < 0.098 && Mbc > 5.273')

but in the plot the '<' and '>' symbols are not recognised and I get: ¿ or ¡ 在此输入图像描述

I was trying also to put them between $$ but it was working... How can I fix this?

Note that each of your label s (after the + is applied) is a string that contains two $ s. < or > means what you want only between such markers, so move one of them to include it.

Also note that & is similarly special, but in that case you can just put a \\ in front of it. (Not that typeset mathematics normally uses && , but rather \\wedge or just “and”.)

This seems to be caused by you using a text font ("serif") that does NOT have the < and > glyphs in the expected places. It instead has the ¿ and ¡ glyphs in the corresponding slots. IIRC, the original CM fonts did that, but I thought "modern" systems use more modern fonts that did not have the limits that the original CM fonts had.

You can put the relevant parts in math mode as Davis Herring suggests in his answer - that is probably the best way because these are math symbols and this insulates you from the craziness of having to find a font that has the "right" glyphs:

label=r'with $\vert \Delta E \vert < 0.039$ \&\& $Mbc > 5.275$')

Or you could use the text versions of < and > but you'd have to worry about the spacing:

label=r'with $\vert \Delta E \vert$  \textless 0.039 \&\& Mbc \textgreater 5.275')

Or you could use a font that has those glyphs in the corresponding slots:

plt.rc('font', family='monospace')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM