带有斜体字的Arial中的Matplotlib xlabel [英] Matplotlib xlabel in Arial with one word italicised

查看:299
本文介绍了带有斜体字的Arial中的Matplotlib xlabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有x轴标签的matplotlib图形,该标签为Arial字体,并且用斜体字表示.

I want to create a matplotlib figure with an x-axis label that is in Arial font, and has one word italicised.

我可以用乳胶字体创建带有x轴标签的图形,并以斜体字表示一个单词;我也可以在Arial中创建带有x轴标签的图形,只要整个标签都为斜体或整个标签为法线即可.但我无法将Arial中的xlabel的部分以斜体显示,将另一部分的Ararial正常的部分.

I can create figures with x-axis labels in latex-font with one word italicised; I can also create figures with x-axis labels in Arial, as long as I have either the entire label italicised or the entire label normal; but I can't get part of the xlabel in Arial italicised and the other part in arial normal.

我主要是尝试以下代码的置换:

I'm mostly trying permutations of the following code:

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()

这篇关于带有斜体字的Arial中的Matplotlib xlabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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