Issue
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?
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()
To use it for x/y ticks or labels, you must use the fontproperties argument:
plt.yticks(fontproperties=font)
Answered By - Oskar Hofmann
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.