2012-07-06 113 views
28

我的问题是我想在某些地块使用乳胶标题,而其他地方不使用乳胶。现在,matplotlib有Latex标题和非Latex标题的两种不同的默认字体,我希望两者保持一致。是否有RC设置,我必须改变,这将自动允许这个?Matplotlib,使用乳胶的一致字体

我产生用下面的代码一个情节:

import numpy as np 
from matplotlib import pyplot as plt 

tmpData = np.random.random(300) 

##Create a plot with a tex title 
ax = plt.subplot(211) 
plt.plot(np.arange(300), tmpData) 
plt.title(r'$W_y(\tau, j=3)$') 
plt.setp(ax.get_xticklabels(), visible = False) 

##Create another plot without a tex title 
plt.subplot(212) 
plt.plot(np.arange(300), tmpData) 
plt.title(r'Some random numbers') 
plt.show() 

这是我讲的不一致。轴刻度标签是薄寻找相对标题:

+0

我相信[这个答案](http://stackoverflow.com/questions/11611374/sans-serif-font-for-axes-tick-labels-with-latex)会帮助你 – 2017-05-02 13:37:03

回答

26

要使tex-style/mathtext文本看起来像常规文本,您需要将mathtext字体设置为Bitstream Vera Sans,

import matplotlib 
matplotlib.rcParams['mathtext.fontset'] = 'custom' 
matplotlib.rcParams['mathtext.rm'] = 'Bitstream Vera Sans' 
matplotlib.rcParams['mathtext.it'] = 'Bitstream Vera Sans:italic' 
matplotlib.rcParams['mathtext.bf'] = 'Bitstream Vera Sans:bold' 
matplotlib.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$') 

如果您希望常规文本看起来像mathtext文本,您可以将所有内容都更改为Stix。这将影响到标签,标题,蜱,等

import matplotlib 
matplotlib.rcParams['mathtext.fontset'] = 'stix' 
matplotlib.rcParams['font.family'] = 'STIXGeneral' 
matplotlib.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$') 

基本思想是,你需要同时设置定期和mathtext字体是相同的,而这样做的方法是有点模糊。您可以看到自定义的字体列表,

sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist]) 

正如其他人所提到的,你也可以通过乳胶在rcParams设置text.usetex渲染你的一切与一个字体,但这是缓慢和不完全必要的。

11

编辑如果你想改变由内而外matplotlib乳胶使用的字体,看看这个页面

http://matplotlib.sourceforge.net/users/usetex.html

其中一个例子是

from matplotlib import rc 
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) 
## for Palatino and other serif fonts use: 
#rc('font',**{'family':'serif','serif':['Palatino']}) 
rc('text', usetex=True) 

只需选择你喜欢的!

如果你想有一个加粗的字体,你可以尝试\mathbf

plt.title(r'$\mathbf{W_y(\tau, j=3)}$') 

EDIT 2

下面将加粗字体默认为您

font = {'family' : 'monospace', 
     'weight' : 'bold', 
     'size' : 22} 

rc('font', **font) 
+0

我想我应该澄清我的题。现在,我认为Latex字体是丑陋的字体,我想将它与非乳胶字体一致。如果地块缩小并放在论文/出版物中,我想要更厚一点的东西,这样我的情节文本仍然可读。 – ncRubert 2012-07-06 18:44:04

+0

@ncRubert gotcha,然后检查我更新的答案。 – nye17 2012-07-06 18:47:00

+0

另一个解释。我想让轴标签变得更厚。它们是图中最不可读的部分。我更新了这个问题来反映这一点。 – ncRubert 2012-07-06 19:11:08