Matplotlib xlabel in Arial with one word italicised

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

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

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

回答1:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!