2013-01-17 67 views
1

在IPython Notebook中显示LaTeX的行以前已经得到了答复,但是您如何在IPython Notebook中绘图时用LaTeX字符串标记图的坐标轴?IPython笔记本:用LaTeX绘图?

+0

你是怎么安装TEX?是MacTex的BasicTex吗? – minrk

回答

7

它在IPython中的工作方式与在独立脚本中的工作方式相同。这个例子来自the docs

import matplotlib as mpl 
import matplotlib.pyplot as plt 
mpl.rc('text', usetex = True) 
mpl.rc('font', family = 'serif') 
plt.figure(1, figsize = (6, 4)) 
ax = plt.axes([0.1, 0.1, 0.8, 0.7]) 
t = np.arange(0.0, 1.0+0.01, 0.01) 
s = cos(2*2*pi*t)+2 
plt.plot(t, s) 

plt.xlabel(r'\textbf{time (s)}') 
plt.ylabel(r'\textit{voltage (mV)}', fontsize = 16) 
plt.title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!", 
     fontsize = 16, color = 'r') 
plt.grid(True) 
plt.savefig('tex_demo') 
plt.show() 

enter image description here

+0

将上面的示例代码丢弃到IPy Notebook单元格中会返回类似以下内容的错误:“RuntimeError:LaTeX无法处理以下字符串:...!LaTeX错误:找不到文件type1cm.sty。我知道IPy Notebook处理LaTeX的方式与单独使用IPython不同。 – user1988816

+1

@ user1988816你在使用什么操作系统,你是如何安装LaTeX的? – tacaswell

+0

@ user1988816:在绘图中,LaTeX由matplotlib处理,而不是IPython。 IPython笔记本具有单独的机制来显示LaTeX,但是当您绘制图时,它们不会被使用。 –