1

I would like to change the font family of the tick labels of a specific axis to a certain font, defined in a .ttf file. I do not want to change this globally, only for the tick labels. Both matplotlib or seaborn specific instructions would do.

I'm not able to find instructions on how to do this. The approach sugggested in this related question is not applicable.

gandreadis
  • 3,004
  • 2
  • 26
  • 38

1 Answers1

1

First, install the font (depends on your operating system). Then specify the font when setting the tick labels using the fontname argument.

fig, (ax1, ax2) = plt.subplots(1, 2)
ax2.set_xticklabels(np.linspace(0, 1, 6), fontname='Suruma')

enter image description here

If you want to change existing tick-labels, you can set the fontname property of the text object, and redraw:

for text_obj in ax2.get_xticklabels():
    text_obj.set_fontname('Suruma')
fig.canvas.draw()
Paul Brodersen
  • 11,221
  • 21
  • 38
  • Thanks! I'd hoped for a option that didn't require installing fonts to the system, but I get that that is probably not possible. – gandreadis Mar 31 '20 at 14:20
  • @gandreadis I didn't realize that was an important requirement. It is possible, just requires a bit more setup. See part 2 in this [answer](https://stackoverflow.com/a/16574948/2912349). – Paul Brodersen Mar 31 '20 at 14:59
  • Also, you can simply add the font to the matplotlib font folder, as discussed [here](https://stackoverflow.com/a/47758634/2912349). – Paul Brodersen Mar 31 '20 at 15:02