Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get XKCD font working in matplotlib

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()
like image 785
baxx Avatar asked Sep 21 '16 10:09

baxx


People also ask

What font is used by matplotlib?

The default font has changed from "Bitstream Vera Sans" to "DejaVu Sans". DejaVu Sans has additional international and math characters, but otherwise has the same appearance as Bitstream Vera Sans.

What is the Xkcd font?

xkcd Script is a font derived from a handwriting sample provided by Randall. It is far less uniform than xkcd , and we think it is therefore more like a true script font. You can see the font as a live preview, or for more information about the font and how it is constructed see the xkcd-script/README.

Where are matplotlib fonts?

To map font names to font files, matplotlib has a dictionary (or json file) located in its cache directory. Note, this file is not always in the same place, but usually sits at the home directory. If you are on mac (windows), it usually sits at whereever your HOME (%HOME%) environmental variable is set to.


3 Answers

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.

like image 166
Learning is a mess Avatar answered Oct 18 '22 01:10

Learning is a mess


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


like image 36
Gui Ambros Avatar answered Oct 18 '22 00:10

Gui Ambros


With matplotlib==3.4.3 I had to do:

import shutil
import matplotlib

shutil.rmtree(matplotlib.get_cachedir())
like image 1
mommi84 Avatar answered Oct 18 '22 01:10

mommi84