Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting xkcd plots using matplotlib

Tags:

matplotlib

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?

like image 216
orome Avatar asked Oct 29 '13 16:10

orome


3 Answers

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

like image 186
Piotr Migdal Avatar answered Nov 20 '22 03:11

Piotr Migdal


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
like image 21
Christoffer Möckelind Avatar answered Nov 20 '22 03:11

Christoffer Möckelind


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

like image 1
Raimundo Jimenez Avatar answered Nov 20 '22 02:11

Raimundo Jimenez