matplotlib pie 显示不了中文

饼状图 的labels 的中文 显示成方框了,plot.title()  也显示不了中文。

$ fc-list :lang=zh #查看可用的中文字体
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/X11/misc/18x18ja.pcf.gz: Fixed:style=ja
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai CN:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai HK:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW:style=Book
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc: 文泉驿微米黑,文泉驛微米黑,WenQuanYi Micro Hei:style=Regular
/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc: 文泉驿正黑,文泉驛正黑,WenQuanYi Zen Hei:style=Regular
/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc: 文泉驿点阵正黑,文泉驛點陣正黑,WenQuanYi Zen Hei Sharp:style=Regular
/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf: Droid Sans Fallback:style=Regular

解决方法分别如下:

1.labels
patches,l_text,p_text=plt.pie(sizes, labels=char, autopct = '%3.1f%%',shadow=True, startangle=90)

for t in l_text:
t.set_fontproperties(matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc")) # 把每个文本设成中文字体


2.title
zhfont = matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc")

plt.title(u"文字统计",fontproperties=zhfont)

3.图例也显示中文
plt.legend(prop=zhfont)

猜你喜欢

转载自jacklin2015.iteye.com/blog/2315715
pie