Skip to content

matplotlib 配置中文字体

2025-06-19 · 111字 · 1分钟

pythonmatplotlib对中文字体的原生支持较弱,为此我们需要使用以下代码指定 matplotlib 用何种字体来渲染中文字符。

python
from matplotlib import rcParams
rcParams['font.family'] = 'Noto Sans CJK SC' # 思源黑体简中版

如果不清楚安装在当前环境的 matplotlib 所支持的全部字体,可以通过以下命令打印出

python
import matplotlib.font_manager
for i in sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist]):
    print(i)
返回

人同此心,心同此理;如风沐面,若水润心