16

The current version of matplotlib (1.3.1) supports xkcd-style plotting, but when I follow the basic instructions for generating such a plot (in iPython 1.1.0),

%pylab
plt.xkcd()
plt.plot(sin(linspace(0, 10)))
plt.title('Whoo Hoo!!!')

I get

enter image description here

instead of

enter image description here

What am I doing wrong?

orome
  • 45,163
  • 57
  • 202
  • 418
  • 3
    Relevant? "Also, if you want to have the font match above, be sure to download and install the Humor Sans font on your system. For matplotlib to recognize it, you may have to remove the font cache, found on your system" – jbabey Oct 29 '13 at 16:33
  • @jabey: Doh! How did I miss that? Is there a current recommended version to get (the link is dead). Also, the missing font alone can't explain the lines, which don't seem to be xkcdified. – orome Oct 29 '13 at 16:39
  • 1
    What backend are you using for matplotlib? You can check with `plt.get_backend()`. Try setting it to a *Agg backend, such as Qt4Agg if you have Qt4 installed. You can also use the 'inline' backend with pylab if you're in an ipython notebook. e: see this issue: https://github.com/matplotlib/matplotlib/issues/2269 – aganders3 Oct 29 '13 at 17:15
  • the `plt.xkcd` command takes a number of parameters to tune the amount of manipulation it performs. see it's docstring for more info. – Paul H Oct 29 '13 at 17:19
  • @aganders3: MacOSX. I can't switch to qt because that leads me into all kinds of bugs trying to install sip. – orome Oct 29 '13 at 17:28
  • @PaulH: Supplying those arguments doesn't have any effect for me. – orome Oct 29 '13 at 17:32
  • I don't think the `plt.xkcd` works with the OSX backend. Try writing your plot to a file with the AGG backend, instead of using an interactive backend. If you need this interactively you will need GTK or Qt or Tk. – aganders3 Oct 29 '13 at 17:32
  • @aganders3: If that's the case, then that's probably the answer to my question. – orome Oct 29 '13 at 17:36
  • I have a similar problem, only that I get the lines and not the font. How do you download the font? Txs – Luis Miguel Nov 07 '13 at 01:59

4 Answers4

20

To get it working, you need

  • matplotlib 1.3.1 (it won't work with matplotlib 1.3.0 or earlier)
    • sudo pip install matplotlib --upgrade
  • font Humor Sans
    • download from here or here, install (on OS X you open it and click Install)
  • remove the matplotlib font cache (as suggested by DanHickstein in matplotlib/issues/2269)
    • rm ~/.matplotlib/fontList.cache

Now, execute the code and it should work.

You do not need to change backend to TkAgg, as some people suggest. For me it works perfectly fine on 'module://IPython.kernel.zmq.pylab.backend_inline' (you can check it by plt.get_backend()).

(I had the same problem and I've solved it, at least on OS X 10.8.5, with matplotlib 1.3.1 and IPython 2.0.0; removing font cache was necessary to get the font running.)

Piotr Migdal
  • 11,864
  • 9
  • 64
  • 86
  • 6
    For matplotlib 1.3.1 on Ubuntu 14.04, the cache is in `~/.cache/matplotlib` (not sure when that changed) and deleting it is indeed vital. – Mark Sep 06 '14 at 16:08
  • Same is true for matplotlib 1.4.2 on Ubuntu 12.04 – dranxo Dec 23 '14 at 22:10
  • maybe also /tmp/xdgcache/matplotlib directory in 1.4.2 version – liuyang1 May 17 '15 at 06:10
  • 1
    matplotlib font cache on Win7: `%userprofile%\.matplotlib\fontList.cache` - [source](http://stackoverflow.com/a/26106170/5276734) – bastelflp Nov 28 '15 at 21:31
  • I also had to delete `fontList.py3k.cache`. – Svaberg Mar 09 '18 at 07:24
  • As suggested below, the font cache can be cleared directly from within Python via `import matplotlib` and then `matplotlib.font_manager._rebuild()`. This should be platform-independent. – Unis Feb 03 '21 at 10:26
4

Using ubuntu 16.04 and python 3, with matplotlib 2.0.0 installed the following fixes the problem for me.

  1. Install Comic sans: sudo apt install fonts-humor-sans
  2. Remove matplotlib cache: rm ~/.cache/matplotlib -r
1

Make sure you have fonts-humor-sans installed.

In Ubuntu/Debian, you can install them with (from the command-line):

$ sudo apt install python3-xkcd fonts-humor-sans

Then the best option (instead of deleting the matplotlib cache) is to rebuild the font manager (from within the Python interpreter, script or Jupyter notebook):

import matplotlib
matplotlib.font_manager._rebuild()

0

From the blog post you linked:

Also, if you want to have the font match above, be sure to download and install the Humor Sans font on your system. For matplotlib to recognize it, you may have to remove the font cache...

As far as which version of the font to download, I would try the most current.

jbabey
  • 45,965
  • 12
  • 71
  • 94
  • 1
    See edited comment above: I've got the font now, but the lines are still not xkcdified. – orome Oct 29 '13 at 16:47
  • @raxacoricofallapatorius are you using the right version of matplotlib? – jbabey Oct 29 '13 at 16:50
  • I think so: added version number to question. – orome Oct 29 '13 at 16:54
  • @raxacoricofallapatorius no idea then, never written anything in python before, just thought it might be the missing font :P – jbabey Oct 29 '13 at 16:59
  • @raxacoricofallapatorius: The matplptlib font cache has to be cleared after the font is installed: http://stackoverflow.com/questions/26085867/matplotlib-font-not-found/26106170#26106170 – bastelflp Nov 28 '15 at 21:33