1

I was trying to use latex in one of my labels for a matplotlib plot. As shown on matplotlib website, using an r in the beginning of the string helps me do that. as shown below:

plt.title(r'$\alpha > \beta$')

But now, I want to replace the inside of the string ('$\alpha > \beta$') with a variable. I tried some combinations but it doesn't work.

Any help is appreciated, Thanks a lot!

1 Answer 1

1

From Python 3.6+, you can use an f-string, which also works with a raw string:

u = 5
plt.title(rf'$\alpha > {u}$')

Otherwise, the str.format function would also work:

plt.title(r'$\alpha > {}$'.format(u))

Output:

enter image description here

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

1 Comment

This doesn't seem to work anymore. Both of these approaches give a ParseException: Expected end of text, found '$' (at char 0), (line:1, col:1) for me, while it does work without putting the variable in....

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.