2016-08-18 185 views
1

我想對齊matplotlib中的等號。因此,我使用matplotlib的eqnarray的環境:在matplotlib中使用latex「eqnarray」命令減少等號之間的間距?

import matplotlib.pyplot as plt 
from matplotlib import rc 

rc('text', usetex=True) 
rc('font', size  = 7) 

fig = plt.figure(figsize=(3,2)) 
ax = fig.add_subplot(111)  

ax.text(0.5,0.5 ,r'\begin{eqnarray*}' +\ 
      r'M    &=& 0.95' + '\\\\' +\ 
      r'\xi    &=& 0.5' + '\\\\' +\ 
      r'\mu    &=& 0.1' + '\\\\' +\ 
      r'a/b    &=& 0' + '\\\\' +\ 
      r'\delta_{99}/L &=& 0' +\ 
      r'\end{eqnarray*}', 
      verticalalignment='center', 
      horizontalalignment='center') 

plt.savefig('output.pdf') 
plt.show() 

結果看起來是這樣的: enter image description here

我怎樣才能減少間距的等號附近?

+0

您可以與= 0.95' + '\\\\'''通過更換r'M的原始字符串'r'M &=&0.95 \\'',這是'r'上的好事;) – Luis

+0

@Luis:在代碼中看起來更好,但輸出仍然相同。 – malohm

+0

好吧,它使用'對齊',以下這種方法:https://stackoverflow.com/questions/30515888/align-latex-math-text-in-matplotlib-text-box – malohm

回答

1

您需要加載amsmath包才能使用'align'。 這裏討論'eqnarray'中的空白問題:https://github.com/matplotlib/matplotlib/issues/4954。至少在matplotlib 1.2.1中,我猜這個問題沒有解決。

這應該給予同樣的結果:

#!/usr/bin/python 
import matplotlib.pyplot as plt 

preamble = { 
    'text.usetex' : True, 
    'font.size' : 7, 
    'text.latex.preamble': [ 
     r'\usepackage{amsmath}', 
     ], 
    } 
plt.rcParams.update(preamble) 

fig = plt.figure(figsize=(3.,2.)) 
ax = fig.add_subplot(111) 

ax.text(0.5,0.5 ,r'\begin{align*}' +\ 
      r'M    &= 0.95 \\' +\ 
      r'\xi   &= 0.5 \\' +\ 
      r'\mu   &= 0.1 \\' +\ 
      r'a/b   &= 0 \\' +\ 
      r'\delta_{99}/L &= 0  ' +\ 
      r'\end{align*}', 
      verticalalignment='center', 
      horizontalalignment='center') 



plt.savefig('output.pdf') 
plt.show() 

enter image description here