8

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)
Gabriel
  • 37,092
  • 64
  • 207
  • 369
Turner Hughes
  • 321
  • 2
  • 3
  • 5
  • 4
    Have you answered your question? If so, then add an answer with your solution, and close the question. – Gary Kerr Sep 01 '11 at 12:18

2 Answers2

10

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.

Alexey Grigorev
  • 2,335
  • 24
  • 43
J Spen
  • 2,534
  • 4
  • 23
  • 39
1

http://matplotlib.sourceforge.net/users/customizing.html

"Customizing matplotlib: matplotlib uses matplotlibrc configuration files to customize all kinds of properties, which we call rc settings or rc parameters. You can control the defaults of almost every property in matplotlib: figure size and dpi, line width, color and style, axes, axis and grid properties, text and font properties and so on." …

kay
  • 24,516
  • 10
  • 94
  • 138
ell
  • 13
  • 3