From: <ef...@us...> - 2010-06-14 02:33:43
|
Revision: 8432 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8432&view=rev Author: efiring Date: 2010-06-14 02:33:37 +0000 (Mon, 14 Jun 2010) Log Message: ----------- [3015614] give examples/api/font_file.py a fighting chance on Linux and Win Modified Paths: -------------- trunk/matplotlib/examples/api/font_file.py Modified: trunk/matplotlib/examples/api/font_file.py =================================================================== --- trunk/matplotlib/examples/api/font_file.py 2010-06-14 01:49:12 UTC (rev 8431) +++ trunk/matplotlib/examples/api/font_file.py 2010-06-14 02:33:37 UTC (rev 8432) @@ -1,19 +1,40 @@ -# -*- noplot -*- -""" -Although it is usually not a good idea to explicitly point to a single -ttf file for a font instance, you can do so using the -font_manager.FontProperties fname argument (for a more flexible -solution, see the font_fmaily_rc.py and fonts_demo.py examples). -""" -import matplotlib.font_manager as fm - -import matplotlib.pyplot as plt - -fig = plt.figure() -ax = fig.add_subplot(111) -ax.plot([1,2,3]) - -prop = fm.FontProperties(fname='/Library/Fonts/Tahoma.ttf') -ax.set_title('this is a special font', fontproperties=prop) -plt.show() - +# -*- noplot -*- +""" +Although it is usually not a good idea to explicitly point to a single +ttf file for a font instance, you can do so using the +font_manager.FontProperties fname argument (for a more flexible +solution, see the font_fmaily_rc.py and fonts_demo.py examples). +""" +import sys +import os +import matplotlib.font_manager as fm + +import matplotlib.pyplot as plt + +fig = plt.figure() +ax = fig.add_subplot(111) +ax.plot([1,2,3]) + +if sys.platform == 'win32': + fpath = 'C:\\Windows\\Fonts\\Tahoma.ttf' +elif sys.platform == 'linux2': + fonts = ['/usr/share/fonts/truetype/freefont/FreeSansBoldOblique.ttf', + '/usr/share/fonts/truetype/ttf-liberation/LiberationSans-BoldItalic.ttf', + '/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf', + ] + for fpath in fonts: + if os.path.exists(fpath): + break +else: + fpath = '/Library/Fonts/Tahoma.ttf' + +if os.path.exists(fpath): + prop = fm.FontProperties(fname=fpath) + fname = os.path.split(fpath)[1] + ax.set_title('this is a special font: %s' % fname, fontproperties=prop) +else: + ax.set_title('Demo fails--cannot find a demo font') +ax.set_xlabel('This is the default font') + +plt.show() + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |