2017-07-05 40 views
2

我已經爲我的x刻度標籤創建了分數值,但分數看起來有點侷促。有沒有辦法增加垂直間距?Matplotlib乳膠分數中的垂直間距

import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 

ax.set_xlim(0, 10) 
ax.set_xticklabels(
    ['$\\frac{{{0}}}{{{1}}}$'.format('M', x) for x in ax.get_xticks()], 
    fontsize=12) 

plt.show() 

enter image description here

+0

以下工作對您有什麼好處? plt.rc('text',usetex = True) plt.rc('font',family ='serif') ax.set_xticklabels( [r'$ \ frac {{{0}}} {{{1 ('M',x)for x in ax.get_xticks()], fontsize = 12) –

+0

@NipunBatra謝謝你,但是這並不能改善。另外,我希望我的類型是san-serif。 – onewhaleid

回答

0

的垂直間隔可以通過強制Matplotlib加以改進,以使用LaTeX而非mathtext

plt.rcParams['text.usetex'] = True 
plt.rcParams['text.latex.preamble'] = [r'\usepackage{sansmath}', r'\sansmath'] 

sansmath包保留無襯線字體:

之前:enter image description here之後:enter image description here

1

使用\dfrac可能做的伎倆。但是,我們需要修改序言。

import matplotlib.pyplot as plt 
plt.rc('text', usetex=True) 
plt.rc('text.latex', preamble=r'\usepackage{amsmath}') 
fig, ax = plt.subplots() 

ax.set_xlim(0, 10) 
ax.set_xticklabels(
    ['$\\dfrac{{{0}}}{{{1}}}$'.format('M', x) for x in ax.get_xticks()], 
    fontsize=12) 

enter image description here

一些更多的信息在這裏:How to write your own LaTeX preamble in Matplotlib?