简体   繁体   中英

Mathematical expressions in Matplotlib 1.3.1

I am trying to write mathematical expressions in matplotlib.pyplot version 1.3.1

>>> import matplotlib
>>> print matplotlib.__version__
    1.3.1

A snippet of the code that does not work is

plt.scatter(np.log10(r1),np.log10(xi1),c='red',label='$\xi$(r) M$_{200}$>13.4')
plt.plot(np.log10(r1),np.log10(curve_y_1),'--',label='fit M200>13.4')

plt.text(0.5,-1.6,'0.0<z<1.0',fontsize=15,color='c')
plt.text(0.5,-1.8,'r$_0$ = %g' % p1_1,fontsize=13,color='r')
plt.text(0.5,-2.0,'$\gamma$ = %g' % p1_2,fontsize=13,color='r')

The entire traceback of the error is:

In [55]: %run correlation_fit_compare.py
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    202             else:
    203                 filename = fname
--> 204             __builtin__.execfile(filename, *where)

ValueError: invalid \x escape

The Matplotib website mentions that anything enclosed within a $ and \\ should give me the required expression. One of their examples is $\\alpha$ which gives them the "math" alpha.

Can anyone tell me how to write \\xi in my label?

EDIT: HAVE POSTED THE ENTIRE plt CODE and the ENTIRE TRACEBACK

When using mathematical text with matplotlib you must use a raw string as opposed to a standard string. In a standard string \\ is used as an escape character, such as \\n representing a new line.

To convert your strings to raw-strings is simple, you'll notice that the matplotlib examples place an r before the start of the string such as r'$\\alpha$' .

In your case you would change it to:

plt.scatter(np.log10(r1),np.log10(xi1),c='red',label=r'$\xi$(r) M$_{200}$>13.4')

In Python, some backslash combinations have special meanings. You can avoid that using raw strings. Also, you should be using only one pair of $ , as it is only one expression:

r'$\xi(r)\ M_{200}>13.4$'

This means "take everything inside the string literally". It is a good idea to always use raw strings involving LaTeX expressions.

Note that the output is a normal string, the r is just telling the parser not to use escape characters.

Note also than in LaTeX, space is indicated by a \\ (slash-space).

Edit

Now you have posted your expressions, I can tell you what is wrong with each one:

  • '$\\xi$(r) M$_{200}$>13.4' : several $ , where you should have only one. See above.
  • 'fit M200>13.4' : No LaTeX here, but it would be r'$\\mathrm{fit}\\ M200>13.4$'
  • 'r$_0$ = %g' % p1_1 : the r should be outside, and the $ enclosing the full expression. r'$r_0 = %g$' % p1_1
  • '$\\gamma$ = %g' % p1_2 : same as above, use only one pair of $ enclosing the full expression: r'$\\gamma = %g$' % p1_2

NOTE: technically, you can mix LaTeX and no LaTeX expressions, but it is error prone, as you can miss a delimiter, and looks ugly.

You can escape the character \\ so it's not interpreted as the escape character \\x, like this:

plt.scatter(np.log10(r1),np.log10(xi1),c='red',label='$\\xi$(r) M$_{200}$>13.4')

Also the parameter label of a plot doesn't actually show anything unless you add a legend to your plot, so after that you will have to either call:

plt.legend()

Or create an xlabel or ylabel instead of labeling the plot:

plt.xlabel(label='$\\xi$(r) M$_{200}$>13.4')
plt.ylabel(label='$\\xi$(r) M$_{200}$>13.4')

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