python matplotlib 中文字体设置
2017-07-17 00:00
751 查看
matplotlib是画图利器,可惜中文支持不够好,不设中文字体画出的图片里面的中文全是小框框,笔者也是google了许多次,尝试了好几种方法,终于弄对了。
思路就是找一个ttf文件,可以直接用Windows系统里面ttf文件,然后创建自己的font,将文字使用的font设置为自己创建的font就可以了。
具体如下
创建好了自己的font,剩下的就是把需要中文显示的地方设成myfont就可以了。
比如饼状图, 饼状图的每一块需要遍历出来单独设置
x, y轴的字体这样设置
legend的字体这样设
是不是超简单, 只要找好了需要的ttf字体文件,将设字体的地方用myfont代替就可以了。
思路就是找一个ttf文件,可以直接用Windows系统里面ttf文件,然后创建自己的font,将文字使用的font设置为自己创建的font就可以了。
具体如下
from matplotlib.font_manager import FontProperties myfont = FontProperties(fname=os.path.join('STSONG.ttf')) //这里请写上自己的ttf路径
创建好了自己的font,剩下的就是把需要中文显示的地方设成myfont就可以了。
比如饼状图, 饼状图的每一块需要遍历出来单独设置
fig1, ax1 = plt.subplots() pie, l_text, p_text = ax1.pie(fracs, colors=COLORS, explode=tuple(explodes), labels=labels, labeldistance=1.1, autopct='%1.2f%%', startangle=90, pctdistance=0.95) for t in l_text: t.set_fontproperties(myfont)
x, y轴的字体这样设置
plt.xlabel('x zhou', fontproperties=myfont) plt.ylabel('y zhou', fontproperties=myfont)
legend的字体这样设
plt.legend(loc=4, borderaxespad=0., prop=myfont)
是不是超简单, 只要找好了需要的ttf字体文件,将设字体的地方用myfont代替就可以了。
相关文章推荐
- python matplotlib中文显示参数设置解析
- python matplotlib 中文显示参数设置
- python画图包seaborn和matplotlib中文字体显示(针对windows系统,,使用ipython notebook)
- python matplotlib 中文显示参数设置
- python matplotlib 中文显示乱码设置
- python matplotlib 画图刻度、图例等字体、字体大小设置
- python matplotlib 画图刻度、图例等字体、字体大小设置
- python matplotlib绘图使用中文字体
- python matplotlib 中文显示参数设置
- python—matplotlib数据可视化实例注解系列-----设置标注字体样式(matplotlib颜色库)
- Matplotlib 中文字体设置
- python matplotlib 中文显示参数设置
- ubuntu下matplotlib中文字体的设置
- python matplotlib 中文显示参数设置
- python matplotlib 无法显示中文
- 解决python27 matplotlib 中文不显示问题
- 使用matplotlib绘图时中文字体的解决方案
- matplotlib画图时的中文设置
- Python中matplotlib图像里的中文输出问题
- 用matplotlib绘制带误差的条形图及中英文字体设置