Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Font Properties to Tick Labels with Matplot Lib

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)
like image 279
Turner Hughes Avatar asked Aug 31 '11 13:08

Turner Hughes


People also ask

How do I make the ticks bold in Matplotlib?

Matplotlib x-axis label boldPass the fontweight parameter to the xlabel() function to make the label bold. We import the matplotlib.


2 Answers

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.

like image 126
J Spen Avatar answered Sep 28 '22 07:09

J Spen


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." …

like image 32
ell Avatar answered Sep 28 '22 06:09

ell