diff --git a/examples/api/font_family_rc_sgskip.py b/examples/api/font_family_rc_sgskip.py index 807f3515b346..8cb4b620cfad 100644 --- a/examples/api/font_family_rc_sgskip.py +++ b/examples/api/font_family_rc_sgskip.py @@ -7,7 +7,7 @@ style (e.g., 'serif', 'sans-serif', or 'monospace'). In the example below, we only allow one font family (Tahoma) for the -san-serif font style. You the default family with the font.family rc +sans-serif font style. You the default family with the font.family rc param, e.g.,:: rcParams['font.family'] = 'sans-serif' diff --git a/examples/api/font_file.py b/examples/api/font_file.py new file mode 100644 index 000000000000..007189151a7a --- /dev/null +++ b/examples/api/font_file.py @@ -0,0 +1,28 @@ +""" +=================================== +Using a ttf font file in Matplotlib +=================================== + +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. + +Here, we use the Computer Modern roman font (``cmr10``) shipped with +Matplotlib. + +For a more flexible solution, see :doc:`/gallery/api/font_family_rc_sgskip` and +:doc:`/gallery/text_labels_and_annotations/fonts_demo`. +""" + +import os +from matplotlib import font_manager as fm, pyplot as plt, rcParams + +fig, ax = plt.subplots() + +fpath = os.path.join(rcParams["datapath"], "fonts/ttf/cmr10.ttf") +prop = fm.FontProperties(fname=fpath) +fname = os.path.split(fpath)[1] +ax.set_title('This is a special font: {}'.format(fname), fontproperties=prop) +ax.set_xlabel('This is the default font') + +plt.show() diff --git a/examples/api/font_file_sgskip.py b/examples/api/font_file_sgskip.py deleted file mode 100644 index f72c039d5c54..000000000000 --- a/examples/api/font_file_sgskip.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -=================================== -Using a ttf font file in matplotlib -=================================== - -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_family_rc.py and fonts_demo.py examples). - -""" -import sys -import os -import matplotlib.font_manager as fm - -import matplotlib.pyplot as plt - -fig, ax = plt.subplots() -ax.plot([1, 2, 3]) - -if sys.platform == 'win32': - fpath = 'C:\\Windows\\Fonts\\Tahoma.ttf' -elif sys.platform.startswith('linux'): - basedir = '/usr/share/fonts/truetype' - fonts = ['freefont/FreeSansBoldOblique.ttf', - 'ttf-liberation/LiberationSans-BoldItalic.ttf', - 'msttcorefonts/Comic_Sans_MS.ttf'] - for fpath in fonts: - if os.path.exists(os.path.join(basedir, 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() diff --git a/examples/text_labels_and_annotations/fonts_demo.py b/examples/text_labels_and_annotations/fonts_demo.py index 11b308297b05..92f5decf38e8 100644 --- a/examples/text_labels_and_annotations/fonts_demo.py +++ b/examples/text_labels_and_annotations/fonts_demo.py @@ -1,13 +1,13 @@ """ -========== -Fonts Demo -========== +================================== +Fonts demo (object-oriented style) +================================== -Show how to set custom font properties. +Set font properties using setters. -For interactive users, you can also use kwargs to the text command, -which requires less typing. See examples/fonts_demo_kw.py +See :doc:`fonts_demo_kw` to achieve the same effect using kwargs. """ + from matplotlib.font_manager import FontProperties import matplotlib.pyplot as plt diff --git a/examples/text_labels_and_annotations/fonts_demo_kw.py b/examples/text_labels_and_annotations/fonts_demo_kw.py index 4f1b7765cb96..f323570cada5 100644 --- a/examples/text_labels_and_annotations/fonts_demo_kw.py +++ b/examples/text_labels_and_annotations/fonts_demo_kw.py @@ -1,12 +1,13 @@ """ -============= -Fonts Demo Kw -============= +=================== +Fonts demo (kwargs) +=================== -Same as fonts_demo using kwargs. If you prefer a more pythonic, OO -style of coding, see examples/fonts_demo.py. +Set font properties using kwargs. +See :doc:`fonts_demo` to achieve the same effect using setters. """ + from matplotlib.font_manager import FontProperties import matplotlib.pyplot as plt import numpy as np