开发者

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.FontPropertie开发者_StackOverflow社区s(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)


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜