I've followed through this post.
I'm trying to reproduce the example from here
This is how mine looks
This is how it should look
The code is on that page and at the end of this post
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
import matplotlib
matplotlib.__version__
Out[37]: '1.5.1'
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
Here is the back end that I'm using
plt.get_backend()
Out[42]: 'TkAgg'
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)
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()
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.
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.
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.
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.
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:~$
With matplotlib==3.4.3
I had to do:
import shutil
import matplotlib
shutil.rmtree(matplotlib.get_cachedir())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With