0
votes

I have tried to install xkcd fonts 'Humor Sans', but unfortunately, I am not able to use the fonts. Here, is what I did so far:

  • iPad Pro
  • installed carnets and juno (for Jupyter notebooks)
  • Matplotlib version 3.1.1
  • installed iFonts to install Humor Sans
  • run xkcd examples, e.g.
%pylab inline
plt.xkcd()
plt.plot(sin(linspace(0,10)))
plt.title('Whoo Hoo!!!')

The plot, with the exception of the fonts is working as expected. The error message gives me, what I expected:

findfont: Font family ['xkcd', 'xkcd Script', 'Humor Sans', 'Comic Sans MS'] not found. Falling back to DejaVu Sans.

The font manager does not find any Humor* Sans although in the Settings -> General -> Fonts, I do find the fonts.

import matplotlib.font_manager
x = [f.name for f in matplotlib.font_manager.fontManager.ttflist if f.name.startswith('Humor')]
print(set(x))

the outcome is an empty result:

set()

Since, the folder structure is not clear using the iPad, i have copied the HumorSans.ttf file in a folder "Fonts", e.g.

import matplotlib.font_manager as fm
import matplotlib.pyplot as plot
import numpy as np

font_path = 'Fonts/Humor-Sans.ttf'
my_font = fm.FontProperties(fname=font_path)

plt.xkcd()
fig, ax = plt.subplot()
x = np.linspace(0,3)
y = np.sin(x)

ax.bar(x, y, color='green')
ax.set_xlabel(u'Some x label', fontproperties=my_font)
ax.set_ylabel(u'Some y label', fontproperties=my_font)
ax.set_title(u'Some fancy title', fontproperties=my_font)

for label in ax.get_xticklabels():
    label.set_fontproperties(my_font)

This works as expected. The fonts is the Humor Sans fonts, where I have explicitly changed the fonts. For all other texts, e.g. yticklabels, the fonts is the standard fonts such that the problem is still there.

How can I correctly install, either directly or load in each notebook the fonts. The problem is that i don't have access to the folders, I need to work in, e.g. matplotlib.get_cachedir() /private/var/mobile/Containers/Data/Application/AAD3....5693/tmp/matplotlib-nu6taz9_

Any help?

1

1 Answers

0
votes

Solved!

Luke Davis Helped with his post: How can I configure matplotlib to be able to read fonts from a local path?

#!/usr/bin/env python3
# Imports
import os
import re
import shutil
from glob import glob
from matplotlib import matplotlib_fname
from matplotlib import get_cachedir

# Copy files over
_dir_data = re.sub('/matplotlibrc$', '', matplotlib_fname())
dir_source = '<your-font-directory-here>'
dir_dest = f'{_dir_data}/fonts/ttf'
# print(f'Transfering .ttf and .otf files from {dir_source} to {dir_dest}.')
for file in glob(f'{dir_source}/*.[ot]tf'):
    if not os.path.exists(f'{dir_dest}/{os.path.basename(file)}'):
        print(f'Adding font "{os.path.basename(file)}".')
        shutil.copy(file, dir_dest)

# Delete cache
dir_cache = get_cachedir()
for file in glob(f'{dir_cache}/*.cache') + glob(f'{dir_cache}/font*'):
    if not os.path.isdir(file): # don't dump the tex.cache folder... because dunno why
        os.remove(file)
        print(f'Deleted font cache {file}.')