在matplotlib中使用TeX和匹配格式编写带有单位的轴标题

时间:2014-02-08 14:30:41

标签: python matplotlib latex

我正在为uni编写实验报告,并决定使用pyplot为其绘制图表。我的一个轴的单位是V ^( - 1),但是在使用代码时,我似乎无法使用上标中的幂写入matplotlib(它只是上标减号):

    plt.xlabel('$1/U (10^5 V ^-1 )$')

如果可能的话,我还想将用于轴标题的字体与用于轴上的值的字体相匹配。我试过放:

plt.rc('text', usetex=True)
plt.rc('font', family='serif')

在我的代码的开头,但这给了我错误:

File "C:\Users\Sam\Documents\Uni\Labs\pytry.py", line 92, in <module> plt.savefig("Xrayplot.png") File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.py", line 561, in savefig return fig.savefig(*args, **kwargs) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\figure.py", line 1421, in savefig self.canvas.print_figure(*args, **kwargs) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wxagg.py", line 85, in print_figure FigureCanvasAgg.print_figure(self, filename, *args, **kwargs) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backend_bases.py", line 2220, in print_figure **kwargs) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.py", line 505, in print_png FigureCanvasAgg.draw(self) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw self.figure.draw(self.renderer) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\figure.py", line 1034, in draw func(*args) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes.py", line 2086, in draw a.draw(renderer) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axis.py", line 1089, in draw renderer) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axis.py", line 1038, in _get_tick_bboxes extent = tick.label1.get_window_extent(renderer) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\text.py", line 753, in get_window_extent bbox, info, descent = self._get_layout(self._renderer) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\text.py", line 320, in _get_layout ismath=False) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.py", line 205, in get_text_width_height_descent renderer=self) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\texmanager.py", line 666, in get_text_width_height_descent dvifile = self.make_dvi(tex, fontsize) File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\texmanager.py", line 413, in make_dvi 'LaTeX: \n\n' % repr(tex)) + report) RuntimeError: LaTeX was not able to process the following string: 'lp' Here is the full report generated by LaTeX:

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

对于上标,在应该超级编写脚本的文本周围添加{}。对于字体我不知道解决方案。

 plt.xlabel('$1/U (10^5 V ^{-1} )$')