2

I have this code and I am trying to only make part of the string bold, not the entire legend string. How can I do that?

I have tried using "\033[1m" (start) and "\033[0;0m" (end) as well as plt.legend(prop=dict(weight='bold'), but again, this is making bold the entire string, but just part of it. Here is an example:

import matplotlib
import matplotlib.pyplot as plt

plt.plot([1,2,3], [4,5,6])
plt.legend(['Test [bold]'])

plt.show()

Inside the second set of brackets, I want 'bold' to actually be bold, not Test. Thanks

dayxx369
  • 332
  • 4
  • 9
  • 1
    Possible duplicate of [Make part of a matplotlib title bold and a different color](https://stackoverflow.com/questions/34937048/make-part-of-a-matplotlib-title-bold-and-a-different-color) – DavidG May 14 '19 at 14:58

1 Answers1

5

matplotlib can render LaTeX! Therefore, you can just use the \bf tag, along with enclosing $ marks to denote a LaTeX expression:

import matplotlib
import matplotlib.pyplot as plt

plt.plot([1,2,3], [4,5,6])
plt.legend([r'Test $\bf{bold}$'])

plt.show()

enter image description here

gmds
  • 19,325
  • 4
  • 32
  • 58