Matplotlib绘图的字体问题

记录一下,方便自己绘图的查阅,首先自己常用的自定义配置可以写到.mplstyle文件中,直接用plt.style.use('my.mplstyle')设置

中英文混用+非斜体文本

在中文论文中,可能出现中英文混用的标签等,参考Matplotlib 中英文及公式字体设置使用Matplotlib内置tex进行配置即可,(英文字体有限通常是times new roman受限于\mathrm命令,宋体+Times new roman(非斜体的文本或者单位)+公式)代码如下:

import matplotlib.pyplot as plt
from matplotlib import rcParams

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

plt.title(r'宋体 $\mathrm{Times \; New \; Roman}\/\/ \alpha_i > \beta_i$')
plt.axis('off')
plt.savefig("usestix.png")

效果:

宋体+Times new roman(非斜体的文本或者单位)+公式

如果想使用其他英文字体的话,非斜体的单位就不能随意定制字体了,参考数学文本公式非斜体文本,可以使用\mathdefault{...} 或者 \mathregular{...}用与普通文本相同的字体设置单位字体,但是上标有些偏上,这个问题没有解决。

import matplotlib.pyplot as plt
from matplotlib import rcParams

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

             
plt.title(r'$\mathregular{Helvetica}\/\/ \alpha_i > \beta_i [\mathdefault{m^3/s}][\mathrm{m^3/s}]$')
plt.axis('off')
plt.savefig("usestix.png")

效果如下:

Helvetica字体

找到任意变换英文字体之后还需要补充更新。目前的这里提到的中英混用还只能限于【中文字体】+Times new roman。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值