문제

I want do use LaTex-signs in my matplotlib-figure. First I used.

pl.ylabel(r'$\pi \rho$',family='Courier New')

That was ok. But now the label I want to use is variable and comes from other *.py file. My idea was to import this file to the file from where the title comes:

import plot
YLabel = "$\pi \rho$"
plot(YLabel)

and in plot.py write:

pl.ylabel(r'%s' %(YLabel),family='Courier New')

But this Error appears:

ValueError: 
$\pi$ $
ho$
      ^
Expected end of text (at char 6), (line:1, col:7)

I already read Text rendering With LaTeX and Writing mathematical expressions from the docs of matplotlib, but it didnt help me.

도움이 되었습니까?

해결책

What's happening actually has nothing to do with the string formatting (i.e. calling x = "blah %s" % YLabel).

It's due to the way you initially define YLabel.

For example, try doing:

x = "\rho"
print x

The "\r" is interpreted as a carriage return and doesn't print. It just prints "ho". (This holds for a number of other escape sequences, e.g. \n, \t, \x, \f, \b, etc.)

To avoid this, you either need to define the original string as a "raw" string:

x = r"\rho"
print x

or explicitly escape the "\r" sequence:

x = "\\rho"
print x
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top