Set Font Properties to Tick Labels with Matplot Lib


Question

I'm trying to change the font of tick labels with matplotlib from the standard font to Times New Roman. I think this should be as easy as changing the font for the title and axis labels, but it's proving to be a little tricky. Currently, I'm just trying to set the font for the x-tick labels, which are dates that are autoformatted (which may be one of my problems, but I'm not sure).

I get the error "no attribute 'set_fontproperties' for Axessubplot" when the relevant snippets of code below are run.

ticks_font = matplotlib.font_manager.FontProperties(family='times new roman', style='normal', size=12, weight='normal', stretch='normal')

fig.autofmt_xdate()
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
for label in ax.get_xticklabels():
    ax.set_fontproperties(ticks_font)

Any help is greatly appreciated.

Thanks.

Update/Edit: Ah, I feel like a goof. Just figured it out and it was so obvious once I realized it. In the context of the snippet above, the answer is:

label.set_fontproperties(ticks_font)
1
7
6/21/2015 10:11:34 PM

You can also set it before running the command with a parameter list if your running a script. Such as below taken from a script of mine:

fig_size = [fig_width, fig_height] 
tick_size = 9
fontlabel_size = 10.5
params = {
    'backend': 'wxAgg',
    'lines.markersize' : 2,
    'axes.labelsize': fontlabel_size,
    'text.fontsize': fontlabel_size,
    'legend.fontsize': fontlabel_size,
    'xtick.labelsize': tick_size,
    'ytick.labelsize': tick_size,
    'text.usetex': True,
    'figure.figsize': fig_size
}
plt.rcParams.update(params)

Or if you want to do it like you had it then run:

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

It is getting each label which has all the text properties that you can set for it.

7
11/29/2016 5:02:45 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon