Proposed changes to matplotlib fonts

Hi John,

I have some proposed changes to the way matplotlib handles fonts. The suggested changes are along the lines of the W3C Cascading Style Sheet, Level 1 (CSS1; document. Note that there is also a Level 2 and 2.1 document.

For fonts, HTML/XML documents can specify 5 font properties for font matching purposes. They are: font-family, font-style, font-variant, font-weight, and font-size. The font_family property is a list of prioritized font names. The nicer fonts are listed first, e.g. ['Lucida Grande', Verdana, Geneva, Lucida, Arial, Helvetica, sans-serif]. The first font name is apparently a very elegent sans-serif font that is common on Mac OS X. Verdana is common to Windows and Helvetica is common on UNIX/Linux. font-style can be normal, italic, or oblique; font-variant is normal or small-caps; font-weight has many options, e.g. normal, semi-bold, bold, etc.; and font-size also has many options, e.g. small, medium, large, smaller, larger, 10pt, 12pt, etc. The CSS1 document also gives a font matching algorithm in case your system doesn't have the exact font available.

The Text object in matplotlib has several similar attributes to the CSS1 font properties, so it would seem that you were aware of this approach. They are fontname, fontangle, fontweight, and fontsize. Given the similarities, my suggestion is to replace fontname with fontfamily and allow it to be a list of prioritized fonts. I also suggest replacing fontangle with fontstyle, adding fontvariant, and using the CSS1 options for these attributes.

The fontfamily approach makes it more likely that the more elegant fonts will be used by matplotlib and that if none of the recommended fonts are available, at least some font closely resembling the requested one will be used, i.e. some type of sans-serif font will be used if requested, instead of a serif font.

The CSS1 options for font weights are: [normal=400, bold=700, 100, 200, 300, 400, 500, 600, 700, 800, 900, bolder, and lighter]. The latter two are relative values. It appears that newer fonts will use these numerical values to indicate their font weight, instead of using a descriptive names which are not uniform across font foundries.

Like the CSS1 options for font weight, the size options are: [xx-small, x-small, small, medium, large, x-large, xx-large, smaller, larger], plus point sizes. One of the benefits of using a name option for size is that the sizes are all relative to the medium size, which can be 10pt, 12pt, 14pt, etc. This would make it easy to change all the fonts in a plot just by changing the definition of the medium font from 12pt to 14pt.

As a final suggestion, we could have the Text object initialize the fontfamily attribute to a sans-serif font list, e.g. the one given above, since that seems to be a popular font family for many users.

Please let me know what you think about this suggestion and if you have any changes to the design.

  -- Paul


Paul Barrett, PhD Space Telescope Science Institute
Phone: 410-338-4475 ESS/Science Software Branch
FAX: 410-338-4767 Baltimore, MD 21218