6

I have the following problem and I am totally new to matplotlib and python: To produce high qualitiy plots for my thesis I want to use matplotlib and I want to use the latex commands for the axes etc. When I try the following example (from http://matplotlib.org/users/usetex.html#usetex-tutorial):

import numpy as np 
import matplotlib.pyplot as plt


# Example data 
t = np.arange(0.0, 1.0 + 0.01, 0.01) 
s = np.cos(4 * np.pi * t) + 2

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

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16) 
plt.title(r"\TeX\ is Number "
          r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
# Make room for the ridiculously large title. 
plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo') 
plt.show()

I get the following message:

  File "test.py", line 21, in <module>
    plt.savefig('tex_demo')
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 561, in savefig
    return fig.savefig(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 505, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1105, in draw
    self.label.draw(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 594, in draw
    self._fontproperties, angle, mtext=self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 241, in draw_tex
    self._renderer.draw_text_image(Z, x, y, angle, gc)
OverflowError: cannot convert float infinity to integer

I'm new to Linux (Ubuntu 14.04) and maybe have done something wrong when installing matplotlib; here is what I have done to install matplotlib etc.:

sudo apt-get install python3
sudo apt-get install python-matplotlib
sudo apt-get build-dep python-matplotlib

I am using texmaker and thought this could be the problem; so I did the following:

sudo aptitude install texlive-fonts-recommended texlive-fonts-extra

If I try any other plots including latex commands I get similar error messages, or plots without any lables for the axes.

7
  • Can you please double check your whitespacing and newlines, and edit your question to correct them if necessary? I fail to see how you managed to not get syntax errors everywhere... Commented Jul 12, 2015 at 23:00
  • 1
    Your traceback seems to be from the python2.7 interpreter. What happens when you run through python3 (by explicitly writing python3 test.py in the terminal)? Also have you checked that you have dvipng and ghostscript available. You should get some output if you run the commands "dvipng --version" and "gs --version" from the terminal Commented Jul 12, 2015 at 23:00
  • 1
    Following on from the comment above - if you want to use Python 3 (wholeheartedly recommended as you're starting out - why use an old version?!) then you need to get matplotlib using the python3-matplotlib ubuntu package, rather than python-matplotlib in your apt-get command. Also, you need to atart the script using python3 so that it runs in Python 3. Commented Jul 13, 2015 at 9:25
  • @J Richard Snape, @or1426: Thanks for your time :) I have installed python3-matplotlib now and checked my ghostscript version (9.10) and dvipng version. By writing python3 test.py to my terminal I get the same massage as before. Commented Jul 13, 2015 at 11:14
  • DonkeyKong have you followed the advice on this page under troubleshooting? In particular I've found deleting the cache often helps with strange problems. Commented Jul 13, 2015 at 11:29

1 Answer 1

4

Your problem has been seen before as reported here on the matplotlib-user mailing list. The resolution is here and the confirmation that it works is here.

In summary, you need to clear the tex.cache directory.

To find where that directory is - go into python and execute matplotlib.get_cachedir()

For a standard Ubuntu 14.04 install - you should get something like the following:

~$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib as mpl
>>> mpl.get_cachedir()
'/home/<username>/.cache/matplotlib'

Then do rm <path/to/cache/from/above>/tex.cache/* and then re-run and it should all work.


Docs issue

Some tips like this are available in the usetex troubleshooting docs section, which lead to here to locate the directory, but they unfortunately don't mention get_cachedir()

Edit Pull Request generated based on this question and merged into the matplotlib docs.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you put in a PR adding a note about that?
@tcaswell Assume PR == Pull Request with a docs update? If so, no problem. P.S. Thanks for your tireless work on matplotlib
Yes, PR -> pull request, sorry for the jargon. Followed the links, always fun to discover your own writing in the internet, but have no memory of writing it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.