赞
踩
我们需要解决两个需求:
环境matplotlib 3.6.3
方法很简单,只需要添加一行,前者是matplolib默认的英文字体,后者是中文字体黑体:
plt.rcParams['font.family'] = ['DejaVu Sans','SimHei']
以此类推可以把英文字体改成SimSun
,英文字体改成Times New Roman
要查看自己的font family,可以使用代码(参考来源:链接):
from matplotlib.font_manager import FontManager
mpl_fonts = set(f.name for f in FontManager().ttflist)
print('all font list get from matplotlib.font_manager:')
for f in sorted(mpl_fonts):
print('\t' + f)
具体示例:
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['DejaVu Sans','SimHei']
# 绘制图像
plt.plot([1, 2, 3, 4, 5], [1, 4, 9, 16, 25])
plt.title('中文和English')
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.show()
效果(图中英文为DejaVu Sans,中文为宋体):
import matplotlib.pyplot as plt
plt.rcParams['mathtext.fontset'] = 'stix' # 设置数学公式字体为stix
plt.rcParams['font.family'] = ['SimSun']
# 绘制图像
plt.plot([1, 2, 3, 4, 5], [1, 4, 9, 16, 25])
plt.title('我是中文English$a+b=c$')
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.show()
这个时候只有公式是英文字体:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。