python - 使用 TeX 和匹配格式在 matplotlib 中编写单位为负幂的轴标题

标签 python matplotlib latex

我正在为大学写一份实验报告,并决定使用 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:

任何帮助将不胜感激。

最佳答案

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

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

关于python - 使用 TeX 和匹配格式在 matplotlib 中编写单位为负幂的轴标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21647242/

相关文章:

python - 如何覆盖函数的全局字典的 __setitem__?

python - 绘制单个 XGBoost 决策树

python - matplotlib:在样式表中设置标题颜色

r - 如何在 R 中的绘图标题中使用两个变量?

vim - 如何在vim中进行键绑定(bind),让用户插入,重复插入?

python - 如何找到给定 Pandas 数据帧索引的位置索引?

python - Django - 系统在哪里确定 "form"是身份验证表单?

python - 如何在 emacs 中打开 IPython 解释器?

python - 猴子修补 pandas 和 matplotlib 以删除 df.plot() 的刺

reactjs - 如何让 MathJax 更快?