Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib fonts in Enthought Canopy

I am using the matplotlib library inside Canopy, and the specific function is xkcd(). This function uses a specific font to plot charts. The font is Comic Sans MS, which if not present, should be downloaded.

/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/font_manager.py:1236: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))

I use the small script below, which checks the presence/absence of the font. If not present, it downloads it.

import os
import urllib2
if not os.path.exists('Humor-Sans.ttf'):
    fhandle = urllib2.urlopen('http://antiyawn.com/uploads/Humor-Sans-1.0.ttf')
    open('Humor-Sans.ttf', 'wb').write(fhandle.read())

The problem is that I still don't get the right font to display. In case there is a problem with the font cache, I do the following:

luis@luis-VirtualBox:~$ rm /home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/fontList.cache

Obtaining the following:

rm: cannot remove ‘/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/fontList.cache’: No such file or directory 

What am I missing?

like image 233
Luis Miguel Avatar asked Nov 26 '13 01:11

Luis Miguel


People also ask

What fonts are available in matplotlib?

Matplotlib can use font families installed on the user's computer, i.e. Helvetica, Times, etc. Font families can also be specified with generic-family aliases like ( {'cursive', 'fantasy', 'monospace', 'sans', 'sans serif', 'sans-serif', 'serif'} ).

How do I add fonts to matplotlib?

Usually, double-click on the . ttf file and then click on the Install button in the window that pops up. Note that Matplotlib handles fonts in True Type Format (. ttf) , so make sure you install fonts ending in .

Can you change the font in matplotlib?

There are multiple ways to change the font style of text in matplotlib plots – You can add a default font for all the plots using rcParams or you can set a font style individually for text components of your axes objects.

How do you change the font of a title in Python?

Matplotlib set title font size In Matplotlib, to set the title of a plot you have to use the title() method and pass the fontsize argument to change its font size.


3 Answers

After a lot of research, and not finding anybody who could help me with my question, I was able to answer my own question. This is what I did:

First, I found exactly where all the fonts are in within matplotlib in the virtual environment of Enthought Canopy:

luis@luis-VirtualBox:~$ find -iname '*.ttf'

A long list is generated, with results similar to this:

./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf
./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/VeraMoBI.ttf
./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/STIXGeneral.ttf
./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/STIXNonUniBol.ttf
./Canopy/appdata/canopy-1.1.0.1371.rh5-x86_64/lib/python2.7/site-packages/canopy/resources/fonts/Inconsolata.ttf

I could not see the 'Humor-Sans-1.0.ttf' file/font anywhere, so I manually downloaded and copied it to the directory:

./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/

Still, the chart was defaulting to another font:

Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))

Then I noticed that the font I had downloaded was 'Humor-Sans-1.0.ttf' and the error messages was referring to 'Humor Sans' and 'Comic Sans' (without the 1.0 appendix). So I made two copies of the same file, inside the same directory and called them 'Humor-Sans.ttf' and 'Comic-Sans.ttf' respectively.

Next, I found where the matplotlib fontCache list resides within my virtual environment:

luis@luis-VirtualBox:~$ find -iname 'fontList.cache'
./.cache/matplotlib/fontList.cache

Then removed the cache:

luis@luis-VirtualBox:~$ rm ./.cache/matplotlib/fontList.cache

After that, I opened my Canopy Editor, opened an iPython notebook, wrote some code, plotted some graphs, and presto, my fonts were right!

final output

Not the most elegant solution, but it worked for me.

like image 155
Luis Miguel Avatar answered Oct 12 '22 18:10

Luis Miguel


This worked for me, and had the benefit of being something I could do from within a jupyter notebook, too:

Just type the following from within the python console (or your jupyter notebook):

matplotlib.font_manager._rebuild()
like image 3
rmadams Avatar answered Oct 12 '22 19:10

rmadams


I have the (unfortunate) requirement of working in a Windows environment and came across the same problem. The one thing I would add to this for those working in Windows is that it is not necessarily the name of the file that is important but the title of the font.

For my problem, downloaded helvetica.ttf and put it into the directory

C:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

However, as the properties of the file listed the font's title as "Helvetica-normal" I needed to make sure that I was specifying

font.sans-serif      : Helvetica-normal

in my matplotlibrc file, even though the name of the file was simply "helvetica.ttf"

like image 2
ramzeek Avatar answered Oct 12 '22 17:10

ramzeek