Home > OS >  How do you change the font of axis tick labels in a custom stylesheet?
How do you change the font of axis tick labels in a custom stylesheet?

Time:02-11

I have made a custom style sheet which is working fine. I specify the font which I would like to use (Helvetica for example) by writing:

font.family: sans-serif

font.serif: Helvetica

This results in plot components like the title and axis labels obeying the prescribed font correctly. However, the axis tick labels (the numbers on the axis) are not changing their font. For instance, in my Helvetica case, the labels of the axis ticks remain serif, while the axis labels and figure title are correctly in sans-serif (Helvetica).

I have not found a parameter to specify the font of the axis ticks (like "xtick.font" for example?). How do I change the font of the axis tick label, or atleast make it the same as the general font, within my style sheet?

CodePudding user response:

Step 1:

from matplotlib import font_manager

Step 2: Define:

xticks_font = font_manager.FontProperties(family='sans-serif')

Step 3:

for tick in ax.get_xticklabels():
    tick.set_fontproperties(ticks_font)

Or if you plot in plt and want to change the fontname specifically, you can use

plt.xticks(fontname = 'Helvetica')

CodePudding user response:

Okay I solved my own problem!

If you have [text.usetex: True] in the StyleSheet, it won't keep the font of the minor tick labels up to date with the font you specify in (for example) [font.serif] and [font.serif: Times New Roman].

I learned this by trying to set the math font to be non-italic ('normal'), which also was not able to be changed in any of the common ways reported online. Once I changed to my StyleSheet to have [text.usetex: False], it allowed me to set the math font by [mathtext.default: regular].

  • Related