python - Matplotlib xlabel Arial 格式,其中一个单词为斜体

标签 python matplotlib latex

我想创建一个 matplotlib 图形,其 x 轴标签采用 Arial 字体,并且有一个斜体字。

我可以用 latex 字体创建带有 x 轴标签的图形,其中一个单词为斜体;我还可以在 Arial 中创建带有 x 轴标签的图形,只要我将整个标签设置为斜体或将整个标签设置为正常;但我无法获得 xlabel 的一部分为 Arial 斜体,另一部分为 arial 正常。

我主要尝试以下代码的排列:

from matplotlib import pyplot as plt
import numpy as n
import matplotlib
from matplotlib.font_manager import FontProperties
font = {'family' : 'Arial',
    'weight' : 'medium',
    'size'   : 20,
    'style'  : 'normal'}
font0 = {'family' : 'Arial',
    'weight' : 'medium',
    'size'   : 20,
    'style'  : 'italic'}


matplotlib.rcParams['mathtext.fontset'] = 'custom'
matplotlib.rcParams['mathtext.rm'] = 'Arial'
matplotlib.rcParams['mathtext.it'] = 'Arial'


matplotlib.rc('font', **font)
#matplotlib.rc('font', **font0)
matplotlib.rc('text', usetex=False)

plt.figure(); plt.plot(n.linspace(0,3,10), n.linspace(0,3,10))
plt.xlabel(r'$\mathit{italics}$ - rest normal')

最佳答案

from matplotlib.pyplot import *

# Need to use precise font names in rcParams; I found my fonts with
#>>> import matplotlib.font_manager
#>>> [f.name for f in matplotlib.font_manager.fontManager.ttflist]

rcParams['mathtext.fontset'] = 'custom'
rcParams['mathtext.it'] = 'Arial:italic'
rcParams['mathtext.rm'] = 'Arial'

fig, ay = subplots()


# Using the specialized math font elsewhere, plus a different font
xlabel(r"$\mathit{Italic}$ $\mathrm{and\ just\ Arial}$ and not-math-font",fontsize=18)
# No math formatting, for comparison
ylabel(r'Italic and just Arial and not-math-font', fontsize=18)
grid()

show()

enter image description here

关于python - Matplotlib xlabel Arial 格式,其中一个单词为斜体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44980904/

相关文章:

python - 翻转图像后,OpenCV putText不起作用

Python MySQLdb : connection. close() VS。光标.close()

python - 我想使用 opencv 删除边界

python - 使用 matplotlib python 和 pandas 绘图

python - 模块 'matplotlib' 没有属性 'verbose'

latex - 减小条形图中 "nodes near coords"中的字体大小 #tikzpicture #pgfplots

latex - 当有足够的高度和宽度时,在进入下一页的两列文档中插入宽图形

latex - 如何检查参数是否是 LaTeX 中的 float

python - 如何编辑 Flask-plus 默认 swagger-UI 文本

python - 在 Cartopy 中更改 map 边界颜色