Matplotlib – What are Fonts?


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:WINDOWSFontsPERBI___.TTFC:WINDOWSFontsARIALUNI.TTFC:WindowsFontsBRLNSR.TTFC:WindowsFontscalibri.ttfC:WINDOWSFontsBOD_PSTC.TTFC:WINDOWSFontsWINGDNG3.TTFC:WindowsFontssegoeuisl.ttfC:WindowsFontsHATTEN.TTFC:WINDOWSFontssegoepr.ttfC:WindowsFontsTCCM____.TTFC:WindowsFontsBOOKOS.TTFC:WindowsFontsBOD_B.TTFC:WINDOWSFontscorbelli.ttfC:WINDOWSFontsTEMPSITC.TTFC:WINDOWSFontsarial.ttfC:WINDOWSFontscour.ttfC:WindowsFontsOpenSans-Semibold.ttfC:WINDOWSFontspalai.ttfC:WindowsFontsebrimabd.ttfC:WindowsFontstaileb.ttfC:WindowsFontsSCHLBKI.TTFC:WindowsFontsAGENCYR.TTFC:WindowsFontstahoma.ttfC:WindowsFontsARLRDBD.TTFC:WINDOWSFontscorbeli.ttfC:WINDOWSFontsarialbd.ttfC:WINDOWSFontsLTYPEBO.TTFC:WINDOWSFontsLTYPEB.TTFC:WINDOWSFontsBELLI.TTFC:WINDOWSFontsYuGothR.ttcC:WINDOWSFontsOpenSans-Semibold.ttfC:WindowsFontstrebucbd.ttfC:WINDOWSFontsOCRAEXT.TTFC:WINDOWSFontsJUICE___.TTFC:WINDOWSFontscomic.ttfC:WindowsFontsVIVALDII.TTFC:WindowsFontsCandarali.ttfC:WINDOWSFontscomici.ttfC:WINDOWSFontsRAVIE.TTFC:WINDOWSFontsLeelUIsl.ttfC:WindowsFontsARIALNB.TTFC:WINDOWSFontsLSANSDI.TTFC:WindowsFontsseguibl.ttfC:WINDOWSFontshimalaya.ttfC:WINDOWSFontsTCBI___
..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
TTFC:WindowsFontsBOD_BLAR.TTFC:WINDOWSFontsebrima.ttfC:WindowsFontsLTYPEB.TTFC:WINDOWSFontsFRABKIT.TTFC:WINDOWSFontsREFSAN.TTFC:WINDOWSFontsgadugi.ttfC:WindowsFontstimes.ttfC:WINDOWSFontsMTCORSVA.TTFC:WINDOWSFontsERASDEMI.TTFC:WindowsFontshimalaya.ttfC:WINDOWSFontsgeorgiai.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

”;

Leave a Reply

Your email address will not be published. Required fields are marked *