Matplotlib如何实现英文为Time New Roman中文为宋体

Matplotlib如何实现英文为Time New Roman中文为宋体

背景:准备大论文但是以前英文的图得重新出

一、对legend()显示的内容的字体进行修改

labelss = plt.legend(loc='upper right').get_texts()
[label.set_fontname('Times New Roman') for label in labelss]
label = labelss[0]
label.set_fontproperties('SimSun')

二、config设置两种字体类型对应中英文

如果你在一个plt显示多个线条,每个线条都设置label后,在最后通过plt.legend()方法显示,在这种情况下,要是中文设置宋体,英文设置Times New Roman,那你呵呵吧,通过set_fontname只会修改整体,而通过set_fontproperties才能对单个label进行修改。具体参考了matplotlib.Text的属性。

python中用matplotlib库画图时,把中文设置为宋体,英文设置为Time New Roman,有时候还需要显示公式。设置方法如下:

import matplotlib.pyplot as plt
from matplotlib import rcParams
import numpy as np

config = {
    "font.family":'serif',
    "font.size": 18,
    "mathtext.fontset":'stix',
    "font.serif": ['SimSun'],
}
rcParams.update(config)

x = np.random.random((10,))

plt.plot(x,label='随机数')
plt.title('中文:宋体 \n 英文:$\mathrm{Times \; New \; Roman}$ \n 公式: $\\alpha_i + \\beta_i = \\gamma^k$')
plt.xlabel('横坐标')
plt.ylabel('纵坐标')
plt.legend()
plt.yticks(fontproperties='Times New Roman', size=18)
plt.xticks(fontproperties='Times New Roman', size=18)
plt.show()

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值