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:
- Installing matplotlib with Fink
- Using basic TrueType (.ttf) fonts like Helvetica
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.
- In FinkCommander, update packages so that you are current.
- In FinkCommander, choose File>Preferences...>Fink>Use
unstable packages. Close the window.
- Choose Source>Selfupdate-cvs
(or your method you usually use to update the package list).
- In the Packages list (table) window, select the Name popup and type in "matplotlib".
A package named "matplotlib-py24" will be shown (or -py23, etc. for
different versions of Python). I decided to use Python 2.4.
- Choose Source>Install and
install matplotlib-py24 from source. The following message is displayed:
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
- Go back to the Preferences and
disable "Use unstable
packages"
after installing. If you do not, then next time you might end up
updating to unstable packages. I prefer using "stable" binary packages.
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
- Copy my Windows XP TrueType (.ttf)
font files into a directory on my Mac
- Set the TTFPATH
environment variable in the shell before running python myplot.py
- Cleared out the .ttffont.cache
file.
- Set Arial as the sans-serif font. Although the typeface is
different from Helvetica, its visual appearance is close enough for my
purposes.
More details for the solution, debugging as well as unsuccessful
attempts follow.
Successful steps for
using .ttf fonts with matplotlib on the Mac
- Copy the TTF files from Windows XP: C:\WINDOWS\Fonts\* to a
directory on your Mac. They are just binary files, and there are no
filename or encoding issues that need to be considered. (Beware that
you are copying the files and not moving them. Doh. Fortunately I just
put them back after I made the mistake.)
- Move the fonts into a folder
/Users/yourname/tmp/TTF
- Export TTFPATH, e.g. in Bash
export=/Users/yourname/tmp/TTF
- Remove the TTF font cache. The file will be recreated the next
time you run the script.
rm ~/.ttffont.cache
- Change the ~/.matplotlibrc
file to use a different font, such as Arial:
~/.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.
- Enable debugging. Change the verbose level in ~/.matplotlibrc:
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.
- Specify a font record (or whatever this is called) in myplot.py;
use color. The
examples/text_themes.py example script uses a font record.
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.
- Use FontBook, part of Panther. In your Applications folder, you
can see
all of the font files that are installed and also the ones that are
disabled. This allowed me to find some of the .dfont and .ttf files
that were available to the system for normal Mac applications. I
thought I might be able to provide these fonts to
matplotlib/Python/TkAgg/X11 but no such luck because the fonts were not
in the right format.
- Use myplot.py to use the "family" argument. You can specify
fonts using this method as well:
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.
- Using the Mac font directories in the TTFPATH didn't help because
.dfont files are not .ttf:
export TTFPATH=/Library/Fonts
python myplot.py
export TTFPATH=/System/Library/Fonts
python myplot.py
# debugging output shows that the directories are searched, however
- Changing ~/.matplotlibrc font.sans-serif to prefer helvetica:
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