Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Italicizing letters and numbers in matplotlib

I am attempting to italicize text in my plot:

import matplotlib.pyplot as plt

plt.plot([1,2,3],[1,1,1], label = '$\it{ABC123}$')
plt.legend()
plt.show()

as shown by Styling part of label in legend in matplotlib

but this only italicized ABC, not 123, which was also noticed, but left unsolved, by Matplotlib italic font cannot be applied to numbers in the legend?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have also tried usetex italic symbols in matplotlib? but that changes the fonts which make this figure incompatible with other figures that I’m making.

I’m on Python 3.10.12 and

matplotlib                3.8.1
matplotlib-inline         0.1.3

how can I italicize both letters and numbers, e.g. in ABC123?

>Solution :

You can use the font_manager:

import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

font = font_manager.FontProperties(style='italic')

plt.plot([1,2,3],[1,1,1], label = 'ABC123')
plt.legend(prop=font)
plt.show()

enter image description here

To use it for x/y ticks or labels, you must use the fontproperties argument:

plt.yticks(fontproperties=font)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading