Writing axes titles with units raised to negative powers in matplotlib with TeX and matching formatting

StackOverflow https://stackoverflow.com/questions/21647242

  •  08-10-2022
  •  | 
  •  

Question

I'm writing a lab report for uni and have decided to use pyplot to draw a graph for it. The units for one of my axes is V^(-1), but I can't seem to get matplotlib to write that with the power in superscript (it only superscripts the minus sign) when using the code:

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

I'd also like to match the fonts used for the axes titles with that used for the values on the axes if at all possible. I've tried putting:

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

at the beginning of my code, but that gives me the error:

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:

Any help would be much appreciated.

Was it helpful?

Solution

For the superscript add { } around the text that should be super scripted. For the fonts i don't know the solution.

 plt.xlabel('$1/U (10^5 V ^{-1} )$')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top