Python-如何设置表中的汉字字体(ax,df)

时间:2017-03-27 08:12:12

标签: python pandas matplotlib dataframe

我有一个结果的pandas数据框,如下所示。

comment_corr=comment_book_content[['price','sale_num','sale_revenue','time_count','uid_num','vouch_count','comment_num']]
comment_corr.columns=[u'价格',u'销售数量',u'销售收入',u'总阅读时间',u'净用户数',u'兑换数',u'评论数',]
comment_corr_df=comment_corr.corr(method='pearson')
comment_corr_df=comment_corr_df.reset_index()
comment_corr_df

enter image description here

现在,我想将这个数据帧保存为数字,这样我就可以把它放在我的remport中。我尝试了以下方法。

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(6, 3))
ax.set_frame_on(False)
ax.xaxis.set_visible(False)  # hide the x axis
ax.yaxis.set_visible(False)  # hide the y axis
ax.set_title(u'书评图书各指标相关性分析',fontproperties=font,fontsize=15)

plt.subplots_adjust(left=0.2, bottom=0.2, right=0.8, top=0.4,hspace=0.1,    wspace=0.3)
plt.tight_layout()

tabla=table(ax, comment_corr_df, rowLabels=  ['']*comment_corr_df.shape[0],colWidths=    [0.17]*len(comment_corr_df.columns),loc='center')  # where df is your data frame
tabla.auto_set_font_size(False) # Activate set fontsize manually
tabla.set_fontsize(10) # if fontsize is necessary ++colWidths
tabla.scale(1.6,1.8) # change size table

但是,此table方法无法成功显示中文字符,如中文字符列名称和行索引,如下所示。 enter image description here

我的问题是如何设置table method,中的字体,以便它通常可以在数据框中显示中文字符

1 个答案:

答案 0 :(得分:0)

你应该在代码的开头有一个编码子句:

# -*- coding: utf-8 -*-

这应该是您代码的第一行。