如何更改 matplotlib (python) 中的字体? [英] How to change fonts in matplotlib (python)?

查看:58
本文介绍了如何更改 matplotlib (python) 中的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来是一个简单的问题,但我没有找到任何有效的解决方案来在 python 中使用 matplotlib 绘制的图中更改字体(而不是字体大小).

It sounds as an easy problem but I do not find any effective solution to change the font (not the font size) in a plot made with matplotlib in python.

我找到了一些教程,通过修改 matplotlib 存储其默认字体的文件夹中的一些文件来更改 matplotlib 的默认字体 - 参见 这篇博文 - 但我正在寻找一个不太激进的解决方案,因为我想在我的绘图中使用不止一种字体(文本、标签、轴标签等).

I found a couple of tutorials to change the default font of matplotlib by modifying some files in the folders where matplotlib stores its default font - see this blog post - but I am looking for a less radical solution since I would like to use more than one font in my plot (text, label, axis label, etc).

推荐答案

假设标题使用 Comic Sans,x 标签使用 Helvetica.

Say you want Comic Sans for the title and Helvetica for the x label.

csfont = {'fontname':'Comic Sans MS'}
hfont = {'fontname':'Helvetica'}

plt.title('title',**csfont)
plt.xlabel('xlabel', **hfont)
plt.show()

这篇关于如何更改 matplotlib (python) 中的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆