17

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()
12
  • Check the answer here. Sep 28, 2016 at 18:44
  • @lincolnfrias what, the one which is the first thing I reference in the post? I've looked ;)
    – baxx
    Sep 28, 2016 at 22:55
  • Oops, sorry. I pointed to that because in my case, removing the matplolib cache in .config solved the problem. Sep 29, 2016 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, 2016 at 22:30
  • 2
    @MartinThoma no I haven't solved this, maybe it'll be added to the millennium prize problems
    – baxx
    Feb 2, 2019 at 13:01

3 Answers 3

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:~$


1
  • 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 at 15:23
6

With matplotlib==3.4.3 I had to do:

import shutil
import matplotlib

shutil.rmtree(matplotlib.get_cachedir())
1
  • 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 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.

4
  • This gives an error in matplotlib 3.6.2
    – MRule
    Jan 25 at 15:19
  • What's the error message? Jan 26 at 11:46
  • I get: "AttributeError: module 'matplotlib.font_manager' has no attribute '_rebuild'"
    – Rich
    May 9 at 9:52
  • Possible solution here May 9 at 21:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.