2011-08-31 40 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,線寬,顏色和樣式,軸,軸和網格屬性,文本和字體屬性等。「 ...