Matplotlib - What are Fonts?



In Matplotlib library ‘Fonts’ refer to the typefaces used for rendering text in plots and visualizations. Fonts play a significant role in customizing the appearance of text elements such as labels, titles, annotations and legends within plots.

Key Aspects of Fonts in Matplotlib library

  • Font Family − Refers to the style or category of the font. Common font families include serif, sans-serif, monospace etc. Each family has its own visual characteristics.

  • Font Style − Determines the appearance of text such as normal, italic or oblique.

  • Font Weight − Specifies the thickness or boldness of the font ranging from normal to various levels of boldness.

Controlling Fonts in Matplotlib

  • Setting Font Properties − We can control font properties for text elements in plots using parameters such as `fontsize`, `fontstyle`, `fontweight` and `fontfamily` in the functions such as `plt.xlabel()`, `plt.title()` etc.

plt.xlabel('X-axis Label', fontsize=12, fontstyle='italic', fontweight='bold', fontfamily='serif')
  • Global Font Configuration − Adjusting font properties globally for the entire plot using `plt.rcParams` allows us to set default font settings for consistency.

plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.size'] = 12

Importance of Fonts in Visualization

  • Readability − The choice of fonts significantly impacts the readability of text elements in plots. Selecting appropriate fonts improves the clarity of visualized information.

  • Aesthetics − Fonts contribute to the overall aesthetics of the plot by affecting its visual appeal and presentation.

  • Emphasis and Style − Different fonts convey various tones and styles allowing users to emphasize specific elements or create a particular visual atmosphere.

Setting Font Properties Globally

We can configure font properties globally for the entire plot using plt.rcParams.

plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.size'] = 12

In the upcoming chapters let’s go through the each parameter of the fonts in detail.

Common Font-related Functions in Matplotlib

The following are the common font related functions in matplotlib library.

plt.rcParams

plt.rcParams in Matplotlib is a dictionary-like object that allows you to globally configure various settings that affect the appearance and behavior of plots and figures. It serves as a central configuration system for Matplotlib, providing a convenient way to set default parameters for different elements in visualizations.

plt.xlabel(), plt.ylabel(), plt.title()

These functions are used for setting font properties for axis labels and titles.

plt.text(), plt.annotate()

These functions are used for specifying font properties for annotations and text elements.

Get a list of all the fonts currently available

To get a list of all the fonts currently available for matplotlib we can use the font_manager.findSystemFonts() method.

Example

from matplotlib import font_manager
print("List of all fonts currently available in the matplotlib:")
print(*font_manager.findSystemFonts(fontpaths=None, fontext='ttf'), sep="")
Output
List of all fonts currently available in the matplotlib:
C:\WINDOWS\Fonts\PERBI___.TTFC:\WINDOWS\Fonts\ARIALUNI.TTFC:\Windows\Fonts\BRLNSR.TTFC:\Windows\Fonts\calibri.ttfC:\WINDOWS\Fonts\BOD_PSTC.TTFC:\WINDOWS\Fonts\WINGDNG3.TTFC:\Windows\Fonts\segoeuisl.ttfC:\Windows\Fonts\HATTEN.TTFC:\WINDOWS\Fonts\segoepr.ttfC:\Windows\Fonts\TCCM____.TTFC:\Windows\Fonts\BOOKOS.TTFC:\Windows\Fonts\BOD_B.TTFC:\WINDOWS\Fonts\corbelli.ttfC:\WINDOWS\Fonts\TEMPSITC.TTFC:\WINDOWS\Fonts\arial.ttfC:\WINDOWS\Fonts\cour.ttfC:\Windows\Fonts\OpenSans-Semibold.ttfC:\WINDOWS\Fonts\palai.ttfC:\Windows\Fonts\ebrimabd.ttfC:\Windows\Fonts\taileb.ttfC:\Windows\Fonts\SCHLBKI.TTFC:\Windows\Fonts\AGENCYR.TTFC:\Windows\Fonts\tahoma.ttfC:\Windows\Fonts\ARLRDBD.TTFC:\WINDOWS\Fonts\corbeli.ttfC:\WINDOWS\Fonts\arialbd.ttfC:\WINDOWS\Fonts\LTYPEBO.TTFC:\WINDOWS\Fonts\LTYPEB.TTFC:\WINDOWS\Fonts\BELLI.TTFC:\WINDOWS\Fonts\YuGothR.ttcC:\WINDOWS\Fonts\OpenSans-Semibold.ttfC:\Windows\Fonts\trebucbd.ttfC:\WINDOWS\Fonts\OCRAEXT.TTFC:\WINDOWS\Fonts\JUICE___.TTFC:\WINDOWS\Fonts\comic.ttfC:\Windows\Fonts\VIVALDII.TTFC:\Windows\Fonts\Candarali.ttfC:\WINDOWS\Fonts\comici.ttfC:\WINDOWS\Fonts\RAVIE.TTFC:\WINDOWS\Fonts\LeelUIsl.ttfC:\Windows\Fonts\ARIALNB.TTFC:\WINDOWS\Fonts\LSANSDI.TTFC:\Windows\Fonts\seguibl.ttfC:\WINDOWS\Fonts\himalaya.ttfC:\WINDOWS\Fonts\TCBI___
..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
TTFC:\Windows\Fonts\BOD_BLAR.TTFC:\WINDOWS\Fonts\ebrima.ttfC:\Windows\Fonts\LTYPEB.TTFC:\WINDOWS\Fonts\FRABKIT.TTFC:\WINDOWS\Fonts\REFSAN.TTFC:\WINDOWS\Fonts\gadugi.ttfC:\Windows\Fonts\times.ttfC:\WINDOWS\Fonts\MTCORSVA.TTFC:\WINDOWS\Fonts\ERASDEMI.TTFC:\Windows\Fonts\himalaya.ttfC:\WINDOWS\Fonts\georgiai.ttf

Get the list of font family (or Name of Fonts)

Here by using the below code we will get the list of the font family i.e. Name of fonts

Example

from matplotlib import font_manager
print("List of all fonts currently available in the matplotlib:")
print(*font_manager.findSystemFonts(fontpaths=None, fontext='ttf'), sep="")
Output
List of all fonts currently available in the matplotlib:
cmsy10
STIXGeneral
STIXSizeThreeSym
DejaVu Sans Mono
STIXGeneral
STIXSizeOneSym
.......................................................................
ITC Bookman
Computer Modern
Times
Palatino
New Century Schoolbook
Advertisements