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?

9
  • 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" Commented Oct 29, 2013 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. Commented Oct 29, 2013 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: github.com/matplotlib/matplotlib/issues/2269 Commented Oct 29, 2013 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. Commented Oct 29, 2013 at 17:19
  • @aganders3: MacOSX. I can't switch to qt because that leads me into all kinds of bugs trying to install sip. Commented Oct 29, 2013 at 17:28

4 Answers 4

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.)

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

6 Comments

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.
Same is true for matplotlib 1.4.2 on Ubuntu 12.04
maybe also /tmp/xdgcache/matplotlib directory in 1.4.2 version
matplotlib font cache on Win7: %userprofile%\.matplotlib\fontList.cache - source
I also had to delete fontList.py3k.cache.
|
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

Comments

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()

Comments

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.

5 Comments

See edited comment above: I've got the font now, but the lines are still not xkcdified.
@raxacoricofallapatorius are you using the right version of matplotlib?
I think so: added version number to question.
@raxacoricofallapatorius no idea then, never written anything in python before, just thought it might be the missing font :P
@raxacoricofallapatorius: The matplptlib font cache has to be cleared after the font is installed: stackoverflow.com/questions/26085867/matplotlib-font-not-found/…

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.