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!

lifezbeautiful
  • 1,160
  • 8
  • 13

1 Answers1

2

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

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74