Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default fonts in Seaborn statistical data visualization in iPython

After multiple problems trying to run stanford.edu/~mwaskom/software/seaborn/ in Anaconda and Enthought for Mac (many problems with dependencies and versions), I was able to run it from an Enthought virtual environment in Ubuntu (running on VirtualBox).

Following some of their tutorials I recreated the following:

enter image description here

But it bothers me that the fonts in use are not the ones designed for Seaborn, but the closest one.

Does anybody has experience in tweaking the font selection in matplotlib? Any good tutorial on how to use matplotlib's font manager?

like image 847
Luis Miguel Avatar asked Dec 24 '13 01:12

Luis Miguel


2 Answers

As Joe notes, Arial isn't installed on Ubuntu by default, but it's easy to install. This is what I do for testing on Travis, which is an Ubuntu environment:

sudo apt-get install msttcorefonts -qq

Seaborn also exposes the font option at the top level of the style control, so you could also easily use one that's installed on your system. As far as I can tell from poking around, you can get a list of possible fonts this way:

import matplotlib as mpl
font_paths = mpl.font_manager.findSystemFonts()
font_objects = mpl.font_manager.createFontList(font_paths)
font_names = [f.name for f in font_objects]
print font_names

Once you've found one you want to use, just set it by doing, e.g.,

sns.set(font="Verdana")

Of course this would have to be done at the top of every script/notebook that's going to generate seaborn plots (which is annoying), so improving the use of non-default styles is on the roadmap for 0.3.

like image 65
mwaskom Avatar answered Oct 02 '22 04:10

mwaskom


Well, if you want to use Arial, you'll need to install the Microsoft core fonts. If I recall correctly, Arial can't be freely redistributed under the same terms as most OSS, so you'll need to agree to the license agreement and install it yourself.

However, in more general terms, you just want to tweak the rc parameters. (Which can be done either at runtime through matplotlib.rc/matplotlib.rcParams or through a .matplotlibrc file.)

For example, seaborn is basically doing this (among other things):

import matplotlib as mpl
mpl.rcParams['font.family'] = 'Arial'

The error is because you don't have the Arial font installed anywhere on your system.

You usually don't want to touch the font manager directly. There are plenty of exceptions, (e.g. using a specific .ttf file) but generally speaking, you'll want to stick to using the fonts installed on your system. You may want to specify a FontProperties instance, but even for that, it's usually easier to specify things through other keyword arguments.

like image 44
Joe Kington Avatar answered Oct 02 '22 05:10

Joe Kington