Python 如何在MatPlotLib中混合使用中文和英文 背景
MatPlotLib是一个很棒的图形包。但有时我需要用中文绘制数据集。我发现了一些问题 使用MatPlotLib表示非英语字体有两种方法 方法1Python 如何在MatPlotLib中混合使用中文和英文 背景,python,matplotlib,fonts,Python,Matplotlib,Fonts,MatPlotLib是一个很棒的图形包。但有时我需要用中文绘制数据集。我发现了一些问题 使用MatPlotLib表示非英语字体有两种方法 方法1 import matplotlib as mpl mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # YaHei is one common Chinese font mpl.rcParams['axes.unicode_minus'] = False # Repair the bug of
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # YaHei is one common Chinese font
mpl.rcParams['axes.unicode_minus'] = False # Repair the bug of representing '-'as "square"
使用此方法,图中显示的所有文本和数字均为中文字体
方法2
不同的是,我预先定义了一些中文字体的路径,并在需要时调用它
from matplotlib.font_manager import FontProperties
chinese = FontProperties(fname=r'/Library/Fonts/Microsoft/SimHei.ttf', size=20)
ax = plt.gca()
ax.set_title(u'能量随时间的变化', fontproperties=chinese)
我的问题
当字符串同时包含中文文本和英文文本时(例如,中文作为变量,并且应附带一些单位:kg,m/s)
所以,我想字符串可以与中文和英文字体混合在一起作为一个单元
有可能做到这一点吗
好的,我对方法1做了一些试验。结果支持了我的猜测。这是我的密码
import matplotlib
mpl.use('pgf') # stwich backend to pgf
import matplotlib.pyplot as plt
plt.rcParams.update({
"text.usetex": True,# use default xelatex
"pgf.rcfonts": False,# turn off default matplotlib fonts properties
"pgf.preamble": [
r'\usepackage{fontspec}',
r'\setmainfont{Times New Roman}',# EN fonts Romans
r'\usepackage{xeCJK}',# import xeCJK
r'\setCJKmainfont{SimSun}',# set CJK fonts as SimSun
r'\xeCJKsetup{CJKecglue=}',# turn off one space between CJK and EN fonts
]
})
plt.rcParams['savefig.dpi']=300
plt.figure(figsize=(4.5, 2.5))
plt.plot(range(5))
plt.text(2.5, 2., "\CJKfontspec{SimHei}{黑体标注}")# Annotation by SimHei
plt.xlabel("宋体坐标标签(units)")# CJK&EN fonts mixed
plt.tight_layout(.5)
plt.savefig('examples.png')
通常,我们希望英文字体为“Times New Roman”,中文字体为“SimSun”。“font.family”定义了全局字体。对于单位,我们可以使用数学公式。但是,该公式没有“Times New Roman”字体。所以我用“stix”来代替“Times New Roman”
希望它能帮助您!我认为现在不可能在一个字符串中分别使用不同的中英文字体。但是你应该知道,有些字体将中文字体和英文字体结合在一起,例如。一般来说,实现你想要的并不容易。
import matplotlib
mpl.use('pgf') # stwich backend to pgf
import matplotlib.pyplot as plt
plt.rcParams.update({
"text.usetex": True,# use default xelatex
"pgf.rcfonts": False,# turn off default matplotlib fonts properties
"pgf.preamble": [
r'\usepackage{fontspec}',
r'\setmainfont{Times New Roman}',# EN fonts Romans
r'\usepackage{xeCJK}',# import xeCJK
r'\setCJKmainfont{SimSun}',# set CJK fonts as SimSun
r'\xeCJKsetup{CJKecglue=}',# turn off one space between CJK and EN fonts
]
})
plt.rcParams['savefig.dpi']=300
plt.figure(figsize=(4.5, 2.5))
plt.plot(range(5))
plt.text(2.5, 2., "\CJKfontspec{SimHei}{黑体标注}")# Annotation by SimHei
plt.xlabel("宋体坐标标签(units)")# CJK&EN fonts mixed
plt.tight_layout(.5)
plt.savefig('examples.png')
import matplotlib.pyplot as plt
rc = {"font.family" : "Times New Roman",
"mathtext.fontset" : "stix",
}
plt.rcParams.update(rc)enter image description here
fig,ax = plt.subplots(dpi = 300)
ax.set_xlabel(r'密度$\mathrm{kg/m}^3$',fontname = 'SimSun',fontsize = 20)
ax.text(0.2,0.8,r'宋体 $\mathrm{Times New Roman}$(正体)',fontname = 'SimSun',fontsize = 20)
ax.text(0.2,0.6,r'宋体 $Times New Roman$(斜体)',fontname = 'SimSun',fontsize = 20)
ax.text(0.2,0.4,r'$\mathrm{m^3}\ m^3$',fontsize = 30)
fig.tight_layout()
plt.show()