1

Matplotlib is capable of parsing latex strings and displaying (to some extent) LaTeX math expressions. It also has a built in "math mode" which similarly displays nice-looking math expressions. To do this one places an r in front of the string, outside the quotes. An example of annotating a plot would be,

plt.xlabel(r'\textbf{time} (s)')

My understanding is that the r allows for proper escaping of special commands or characters like \textbf or \alpha.

How do I do this when I'm not writing the string in my code? I'm reading a Gtk.Entry to get the string I want and I want to give that string to matplotlib to be interpreted as a LaTeX string. In the example above I would instead have,

string = Gtk.Entry.get_text()
plt.xlabel(r+string)              #how would I write this part?

Any help would be lovely.

5
  • The r tells python to not treat the '\' as an escape in the string literal or you would have to write \\textbf{time} (s) where the first '\' escapes the second (or the string literal wolud be '[tab]extbf{time} (s)'). If you have a string from Gtk it should 'just work'. Commented Nov 18, 2015 at 0:08
  • annoyingly backslash also escapes the flavor of md that SO uses. Commented Nov 18, 2015 at 0:09
  • I don't understand your second comment. Could you elaborate? I tried using double backslash, i.e., ax.text(0, 0.9,'\\textbf{hello}', transform=ax.transAxes, usetex=True) but got a runtime error, 'RuntimeError: In select_charmap: Could not set the charmap'. EDIT: I also tried handing the Gtk.Entry.get_text() to ax.text() with the option usetex=True and got the same runtime error. I can give you the full trace if necessary. Commented Nov 18, 2015 at 9:14
  • Oh, SO=stack overflow, never mind, I understand your second comment now. I do, however, still have my runtime error. Any help would be great. Commented Nov 18, 2015 at 9:21
  • The runtime error disappears when I place a string inside of $$. If I do not include these I get the runtime error above. Is this expected and intentional behaviour? Commented Nov 19, 2015 at 17:39

2 Answers 2

1

Okay, so it turns out that the problem was that, like LaTeX, math needs to be in the math environment by bracketing the text with $insert math here$.

Sign up to request clarification or add additional context in comments.

Comments

0

As the tags indicates Python-3.x I think you could do something like:

plt.xlabel(mystring.encode('unicode_escape').decode('utf'))

being mystring the value returned by Gtk.Entry.get_text() and being mystring a str instance in Python3.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.