You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Matplotlib中使用TeX时如何显示中文标题?

问题:Matplotlib启用TeX渲染时中文标题无法显示

尝试绘制包含中文标题的子图,启用TeX渲染后出现LaTeX中文支持错误,代码如下:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,1000)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.sin(pow(x,2))
plt.rc('font',size = 16,family = 'Simhei')
plt.rcParams['axes.unicode_minus'] = False
plt.rc('text',usetex = True)

pic1 = plt.subplot(2,2,1)
pic1.plot(x,y1,'r--',label = '$\sin{x}$'); pic1.legend()
pic1.set_title('第一个图像')

pic2 = plt.subplot(2,2,2)
pic2.plot(x,y2,'k--',label = '$\cos{x}$') ; pic2.legend()
pic2.set_title('第二个图像')

pic3 = plt.subplot(2,1,2)
pic3.plot(x,y3,'r--',label = '$\sin{(x^2)}$') ; pic3.legend()
pic3.set_title('第三个图像')
plt.show()

报错信息:

(c:/texlive/2022/texmf-dist/tex/latex/type1cm/type1cm.sty)
(c:/texlive/2022/texmf-dist/tex/latex/cm-super/type1ec.sty
(c:/texlive/2022/texmf-dist/tex/latex/base/t1cmr.fd))
(c:/texlive/2022/texmf-dist/tex/latex/base/inputenc.sty)
(c:/texlive/2022/texmf-dist/tex/latex/geometry/geometry.sty
(c:/texlive/2022/texmf-dist/tex/latex/graphics/keyval.sty)
(c:/texlive/2022/texmf-dist/tex/generic/iftex/ifvtex.sty
(c:/texlive/2022/texmf-dist/tex/generic/iftex/iftex.sty)))
(c:/texlive/2022/texmf-dist/tex/latex/underscore/underscore.sty)
(c:/texlive/2022/texmf-dist/tex/latex/base/textcomp.sty)
(c:/texlive/2022/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)
No file b09c0b82eaeea985330356c98a53236d.aux.
*geometry* driver: auto-detecting
*geometry* detected driver: dvips

! LaTeX Error: Unicode character 第 (U+7B2C)
               not set up for use with LaTeX.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.29 {\rmfamily 第
                   $1$个图像}%
No pages of output.
Transcript written on tmpntafuhs9/b09c0b82eaeea985330356c98a53236d.log.

已知LaTeX中可用\usepackage{ctex}解决中文支持问题,但不清楚Matplotlib中如何配置,需要在启用TeX的同时显示中文标题。


解决方案

当Matplotlib启用usetex=True时,所有文本渲染都会交给系统LaTeX处理,此时Matplotlib自身的字体配置(如Simhei)不再生效,必须通过配置LaTeX预导言来添加中文支持:

  • 配置Matplotlib的TeX预导言,加入ctex宏包,让LaTeX能够识别中文Unicode字符
  • 无需保留Matplotlib的font.family配置,交由ctex处理中文字体

修改后的完整代码:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.sin(np.power(x, 2))

# 配置TeX预导言,添加ctex宏包支持中文
plt.rcParams['text.latex.preamble'] = r'\usepackage{ctex}'
plt.rc('text', usetex=True)
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题
plt.rc('font', size=16)  # 仅设置字体大小,字体交由ctex处理

pic1 = plt.subplot(2,2,1)
pic1.plot(x, y1, 'r--', label='$\sin{x}$')
pic1.legend()
pic1.set_title('第一个图像')

pic2 = plt.subplot(2,2,2)
pic2.plot(x, y2, 'k--', label='$\cos{x}$')
pic2.legend()
pic2.set_title('第二个图像')

pic3 = plt.subplot(2,1,2)
pic3.plot(x, y3, 'r--', label='$\sin{(x^2)}$')
pic3.legend()
pic3.set_title('第三个图像')

plt.tight_layout()  # 自动调整子图间距,避免标题重叠
plt.show()

关键说明

  1. plt.rcParams['text.latex.preamble'] = r'\usepackage{ctex}':向LaTeX模板中添加ctex宏包,使其支持中文Unicode字符的渲染
  2. 启用usetex=True后,Matplotlib的font.family配置会被忽略,中文字体由ctex自动处理(默认会调用系统中合适的中文字体)
  3. 保留axes.unicode_minus配置,避免负号显示异常

内容的提问来源于stack exchange,提问作者Zequan Bear

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:42:09