16

I've followed through this post.

I'm trying to reproduce the example from here

This is how mine looks

enter image description here

This is how it should look

enter image description here

The code is on that page and at the end of this post

System that I'm using

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:    14.04
Codename:   trusty

Python version that I'm using

3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

Matplotlib version

import matplotlib
matplotlib.__version__
Out[37]: '1.5.1'

Humorsans font installed on system

This shows that I have installed the font

$ fc-list | grep "Humo"
/usr/share/fonts/Humor-Sans-1.0.ttf: Humor Sans:style=Regular
/home/vco/.fonts/Humor-Sans-1.0.ttf: Humor Sans:style=Regular

Matplotlib backend that I'm using

Here is the back end that I'm using

  plt.get_backend()
  Out[42]: 'TkAgg'

Output error

I've followed the instructions from a post here;

Error from python output:

/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1288:
UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not
found. Falling back to Bitstream Vera Sans

  (prop.get_family(), self.defaultFamily[fontext]))

/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1298:
  UserWarning: findfont: Could not match :family=Bitstream Vera
  Sans:style=normal:variant=normal:weight=400:stretch=normal:size=medium.
  Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmtt10.ttf UserWarning)

I also tried to move the downloaded Humorsans font to

/usr/share/matplotlib/mpl-data/fonts/ttf

But still got the following error

/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1288: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1298: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=400:stretch=normal:size=medium. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmtt10.ttf
  UserWarning)
/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1298: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=400:stretch=normal:size=large. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmtt10.ttf
  UserWarning)

code

from matplotlib import pyplot as plt
import numpy as np

plt.xkcd()

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.xticks([])
plt.yticks([])
ax.set_ylim([-30, 10])

data = np.ones(100)
data[70:] -= np.arange(30)

plt.annotate(
    'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
    xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))

plt.plot(data)

plt.xlabel('time')
plt.ylabel('my overall health')

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.bar([-0.125, 1.0-0.125], [0, 100], 0.25)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.set_xticks([0, 1])
ax.set_xlim([-0.5, 1.5])
ax.set_ylim([0, 110])
ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT'])
plt.yticks([])

plt.title("CLAIMS OF SUPERNATURAL POWERS")

plt.show()
Community
  • 1
  • 1
baxx
  • 3,956
  • 6
  • 37
  • 75
  • Check the answer [here](http://stackoverflow.com/questions/19663986/getting-xkcd-plots-using-matplotlib/22812176#22812176). – lincolnfrias Sep 28 '16 at 18:44
  • @lincolnfrias what, the one which is the first thing I reference in the post? I've looked ;) – baxx Sep 28 '16 at 22:55
  • Oops, sorry. I pointed to that because in my case, removing the matplolib cache in .config solved the problem. – lincolnfrias Sep 29 '16 at 20:15
  • @lincolnfrias "in my case..." < what is this referring to ? You've tested the code that I've posted on a similar setup or is this a different thing you're referring to? Cheers – baxx Sep 29 '16 at 22:30
  • Not the same setup and code, but also on Ubuntu (16.04). – lincolnfrias Oct 01 '16 at 03:53
  • @lincolnfrias so you have different setup, code, and a different version of Ubuntu. Can you let me know what I'm expected to do with the information you've given please? – baxx Oct 01 '16 at 16:56
  • In home directory I have a .config folder containing a matplotlib folder. There I removed the file fontList.cache. In other setups people have instead a separate .matplotlib folder (in the first level of their home, not inside the .config). This is detailed in the answer (and the commentaries to it) to the post I linked above. – lincolnfrias Oct 02 '16 at 12:35
  • @lincolnfrias please read my post, specifically the part "I've followed through this post" that links what you're referencing. If you have a working solution then please post it with code and system setup, otherwise these comments are useless. – baxx Oct 02 '16 at 12:40
  • Did you finally solve it? – Martin Thoma Jun 24 '18 at 12:39
  • 2
    @MartinThoma no I haven't solved this, maybe it'll be added to the millennium prize problems – baxx Feb 02 '19 at 13:01
  • @baxx, could there be another cache where matplotlib is looking apart from the ~/.config/matplotlib, ~/.cache/matplotlib or ~/.matplotlib? – thoroc Feb 21 '19 at 11:18
  • @thoroc I'm not sure at the moment, I've not looked at this for a long time – baxx Feb 21 '19 at 14:09

3 Answers3

6

Try deleting Matplotlib cache and see if it works (i.e., rm -r ~/.cache/matplotlib).

In my case it solved the problem:

user@host:~$ ./xkcd.py
/home/user/.pyenv/versions/3.7.1/lib/python3.7/site-packages/matplotlib/font_manager.py:1241:
UserWarning: findfont: Font family ['xkcd', 'Humor Sans', 'Comic Sans MS'] not found.
Falling back to DejaVu Sans. (prop.get_family(), self.defaultFamily[fontext]))
user@host:~$ rm -r ~/.cache/matplotlib
user@host:~$ ./xkcd.py
user@host:~$


Gui Ambros
  • 1,045
  • 1
  • 12
  • 20
  • 1
    This worked for me (Python 3.10, Ubuntu 22.04, matplotlib 3.6.2). I had to restart the notebook kernel for it to take effect. – MRule Jan 25 '23 at 15:23
6

With matplotlib==3.4.3 I had to do:

import shutil
import matplotlib

shutil.rmtree(matplotlib.get_cachedir())
mommi84
  • 614
  • 1
  • 9
  • 17
  • This worked for me (Python 3.10, Ubuntu 22.04, matplotlib 3.6.2). I had to restart the notebook kernel (and remember to remove the lines that deleted the cache so they don't run again) for it to take effect. – MRule Jan 25 '23 at 15:23
2

If you still see an error after installing HumorSans font, I advise running:

import matplotlib
matplotlib.font_manager._rebuild()

It will rebuild the font cache.

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71