Installing and using matplotlib 0.65 on Fink/Mac OS X 10.3 (Panther); how to use more TrueType fonts.

Updated: December 27, 2004


Installing matplotlib on Fink (Mac OS X 10.3)

Some steps for getting matplotlib to install on Mac OS X using Fink. The main problems I encountered:
The installation instructions on the matplotlib site are helpful. I had not used Python or matplotlib before the week I wrote this so if the terminology is wrong or something seems strange, chalk it up to my neophyte status.

The following instructions assume Mac OS X 10.3, Fink, X11, and FinkCommander are working.

Installing the matplotlib and Python 2.4 packages

Problem: using FinkCommander (0.5.3), the package list doesn't display matplotlib, so it's not clear how to install it.

Solution: install the "unstable" package from source.

Normally I use FinkCommander to install stable packages. The matplotib package ported to Fink is listed as "unstable" so FinkCommander preferences need to be changed to enable download of those packages. It is easy to install this "unstable" package from source and to leave everything else in "stable" mode.

The following package will be installed or updated:
matplotlib-py24
The following 4 additional packages will be installed:
numarray-py24 numeric-py24 pygtk2-py24 pygtk2-py24-dev

After the installation is finished, Python 2.4 and the matplotlib library are ready to be used.

Testing the installation

A few quick tests to show that Python and matplotlib are installed correctly.

Python

From the command line, e.g. within xterm, type "python". You can see the interpreter version at startup:

> python
Python 2.4 (#1, Dec 19 2004, 09:31:53)

[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
>>> ^D
>

Matplotlib

Set up the configuration file to use a backend that works on the Mac, namely "TkAgg", as mentioned in an email by the author of matplotlib, John Hunter. (Eventually I was able to get both TkAgg and GTKAgg to work just by changing the "backend:" field in the .matplotlibrc config file.)

# copy the configuration file and change "GTKAgg" to "TkAgg":
cp /sw/share/matplotlib-py24/.matplotlibrc ~/.matplotlibrc

# go into the examples directory
cd /sw/share/doc/matplotlib-py24/examples

# run a simple bar graph example
# it's gratifying to see the plot the first time!

python barstacked-test.py

# note that some of the example programs will not work if they are
# running directly from the /sw/share/doc/ because write permissions
# are not enabled for that directory, so copy the examples locally:

mkdir ~/tmp
cp -pR /sw/share/doc/matplotlib-py24/examples ~/tmp/matplotlib-examples

cd ~/tmp/matplotlib-examples
python simple-plot.py

# exit by closing the window, then open the newly create PNG file:
open simple-plot.png

# write to .ps or .png. In a Python script using matplotlib add the following:
savefig('filename.ps')
open filename.ps # Mac will automatically convert to PDF
# or
savefig('filename.png')

Using more TrueType fonts on matplotlib on Fink

Problem: Can't use any other fonts for labeling (also known as "you can use any font you like as long as it is Vera Sans")

I am interested in using the standard Helvetica and Times fonts so that I can use fonts that look like the base PostScript fonts. The problem is that matplotlib seems to want to use TrueType fonts, but not the kind that is normally available on the Mac. Using Fink, X11 will provide a number of TrueType fonts, but these do not include a lot of fonts on the Mac. Furthermore, the Mac uses TrueType, but the ".dfont" files are not compatible with the TrueType renderer found in Fink/X11. There is probably a way to convert ".dfont" and other Mac TrueType fonts into .ttf but the following solution worked for me.

Solution: Copy Windows .ttf fonts to the Mac and use the TTFPATH environment variable

More details for the solution, debugging as well as unsuccessful attempts follow.

Successful steps for using .ttf fonts with matplotlib on the Mac

/Users/yourname/tmp/TTF
export=/Users/yourname/tmp/TTF
rm ~/.ttffont.cache
~/.matplotlibrc:
font.sans-serif   : Arial, sans-serif

Debugging how fonts are used in matplotlib (TkAgg)

Some tips for debugging the font problem. There's a number of ways to specify fonts but the first few attempts produced plots with the default (Vera Sans) font.
verbose.level : debug

The next time you run a plotting script, the output will show you whether the TTF font cache is used, which fonts are found or not found and which ones were used to render the plot.

font = {'fontname' : 'sans-serif',
        'color'    : 'b'}  # use blue instead of default red

title('a title for the plot', font)

After running the script, I could see that the color of the font was changed, so I knew this was a way to specify/override the default font family/name.

title('a title for the plot', family='serif')
# not clear if "family" is recognized or ignored

Unsuccessful attempts at getting Mac fonts to work on matplotlib

These are some steps that didn't work for me.
export TTFPATH=/Library/Fonts
python myplot.py

export TTFPATH=/System/Library/Fonts
python myplot.py
# debugging output shows that the directories are searched, however

font.sans-serif   : Helvetica, sans-serif

There is no "Helvetica" .ttf file installed. So I changed this field to use Arial instead.


you at cs.ucsc.edu