2011-08-31 81 views
5

我正在尝试将matplotlib中的刻度标签的字体从标准字体更改为Times New Roman。我认为这应该与改变标题和轴标签的字体一样简单,但是它证明有点棘手。目前,我只是想设置x-tick标签的字体,这是自动生成的日期(这可能是我的一个问题,但我不确定)。将字体属性设置为使用Matplot Lib刻录标签

当下面的相关代码片段运行时,我得到错误“Axessubplot的no属性set_fontproperties”。

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) 

任何帮助,非常感谢。

谢谢。

更新/编辑:啊,我觉得自己像个混蛋。刚刚弄清楚,一旦意识到这一点就显而易见了。在片段的上述背景下,答案是:

label.set_fontproperties(ticks_font) 
+4

您是否回答了您的问题?如果是这样,那么在您的解决方案中添加一个答案,并结束问题。 –

回答

4

你也可以用参数列表运行命令,如果你在运行一个脚本面前。如下面从我的剧本拍摄:

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) 

或者,如果你想要做你喜欢有它然后运行:

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

它越来越具有所有的文本属性每个标签,你可以为它设置。

1

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

定制matplotlib: matplotlib使用matplotlibrc配置文件来定制各种属性,我们称之为RC设置或RC参数可以控制在matplotlib几乎每个属性的默认值:图形大小和dpi,线宽,颜色和样式,轴,轴和网格属性,文本和字体属性等。“ ...

相关问题