在 Matplotlib 中使用 stix 字体制作斜体和粗体标签 [英] Make a label both Italic and Bold style in Matplotlib with stix font

查看:109
本文介绍了在 Matplotlib 中使用 stix 字体制作斜体和粗体标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib生成图,并且我使用了'stix'字体(rcParams ['mathtext.fontset'] ='stix'),以便使字体大小平滑地从文本过渡到数学文本.但是,我想将某些数学符号设为斜体(标量值),将某些数学符号设为斜体 AND 粗体(张量).我不想通过使用 Latex 渲染的解决方案,然后其他事情就搞砸了.

我会给你一个描述问题的小例子:

从numpy导入

  *从matplotlib.pyplot导入*# 将字体更改为 stixrcParams['mathtext.fontset'] = 'stix'# 一些数据来构造这个绘图示例数据x = [0,1,2]数据=[8,9,10]datay2 = [8,15,10]无花果,ay = subplots()ay.plot(datax,datay,color ="0.",ls ='-',label = r"$ F _ {\ alpha} $")ay.plot(datax,datay2,color ="0.",ls ='-',label = r"$ \ mathbf {F _ {\ alpha}} $")# 现在添加一些自定义的图例.图例= ay.legend(loc ='left',shadow = True)#frame = legend.get_frame()#frame.set_facecolor('0.90')xlabel(r"x 标签",fontsize=18)ylabel(r'y label',fontsize = 18)网格()表演()

如果您运行代码,第一个标签是斜体,第二个标签是粗体.如何获得第二个标签为"Bold AND Italic"?

请注意,我也在轴标签中使用了 mathbf.可能会将标签的其余部分更改为 STIX 字体,您可以将其定义为非斜体非粗体:请参阅 文档 位于自定义字体"下.

I am trying to generate a plot with matplotlib and I use the 'stix' font (rcParams['mathtext.fontset'] = 'stix') in order to have smooth font size transitions from text to math text. However, some of my math symbols I want to be Italic (scalar values) and some to be Italic AND Bold (tensors). I do not want to go through the solution of using Latex rendering cause then other things are messed up.

I will give you a small example that depicts the problem:

from numpy import *
from matplotlib.pyplot import * 

# Chaning font to stix
rcParams['mathtext.fontset'] = 'stix'

# Some data to constract this plotting example
datax=[0,1,2]
datay=[8,9,10]
datay2=[8,15,10]

fig, ay = subplots()

ay.plot(datax, datay, color="0.", ls='-', label= r"$F_{\alpha}$")
ay.plot(datax, datay2, color="0.", ls='-', label=r"$\mathbf{F_{\alpha}}$")

# Now add the legend with some customizations.
legend = ay.legend(loc='left', shadow=True)

#frame = legend.get_frame()
#frame.set_facecolor('0.90')
xlabel(r"x label",fontsize=18)
ylabel(r'y label', fontsize=18)
grid()

show()

If you run the code the first label is Italic and the second label is Bold. How could I achieve the second label to be Bold AND Italic?

Problem with math text to be Italic and bold

解决方案

A few more specific mathtext params are needed:

from numpy import *
from matplotlib.pyplot import *

# Changing font to stix; setting specialized math font properties as directly as possible
rcParams['mathtext.fontset'] = 'custom'
rcParams['mathtext.it'] = 'STIXGeneral:italic'
rcParams['mathtext.bf'] = 'STIXGeneral:italic:bold'

# Some data to construct this plotting example
datax=[0,1,2]
datay=[8,9,10]
datay2=[8,15,10]

fig, ay = subplots()

ay.plot(datax, datay, color="0.", ls='-', label= r"$\mathit{F_{\alpha}}$")
ay.plot(datax, datay2, color="0.", ls='-', label=r"$\mathbf{F_{\alpha}}$")

# Now add the legend with some customizations.
legend = ay.legend(loc='left', shadow=True)

# Using the specialized math font again
xlabel(r"$\mathbf{x}$ label",fontsize=18)
ylabel(r'y label', fontsize=18)
grid()

show()

Note that I used the mathbf in an axis label, too. Would probably change the rest of the label to a STIX font, which you can define a non-italic-non-bold case of: see the docs under 'Custom fonts'.

这篇关于在 Matplotlib 中使用 stix 字体制作斜体和粗体标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆