Matplotlib - Font Indexing



In Matplotlib library font indexing refers to accessing and utilizing different fonts or typefaces available for rendering text elements within plots. Matplotlib library provides access to various fonts and understanding font indexing involves knowing how to use these fonts by their names or indices. This is particularly relevant when we want to use fonts that are not in the default set or if we need to use system-specific fonts.

The font.family parameter in Matplotlib accepts either a string representing a known font family or an integer representing a specific font index. The numeric indices are used to reference fonts that are registered with Matplotlib library.

Font Indexing in Matplotlib

The following are the font indexing ways in matplotlib library.

Font Families

  • Matplotlib library offers several font families such as 'serif', 'sans-serif', 'monospace' and more.

  • These font families have their unique characteristics and are used to define the general design of the typeface.

Font Names

  • Matplotlib library allows the use of specific font names to access particular fonts available on our system.

  • Font names enable the selection of custom or installed fonts for rendering text in plots.

Accessing Fonts by Name

To access fonts in Matplotlib we can use both the font names and their indices if available.

Font Names

We can access the fonts available in matplotlib by using the font names. Here is the example of accessing the font 'Times New Roman' by using the plt.rcParams['font.family'] method.

Example

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'Times New Roman'
# Creating a data
x = [i for i in range(10,40)]
y = [i for i in range(30,60)]
# Creating a plot
plt.scatter(x,y,color = 'blue')
plt.xlabel('X-axis cube values')
plt.ylabel('Y-axis cube values')
plt.title('Title with Times New Roman Font')
plt.show()
Output
Times New Roman

Available Fonts on the System

Matplotlib library can also uses the available fonts on our system. The fonts available might vary based on the operating system such as Windows, macOS, Linux and also the installed font libraries.

Indexing Fonts with findSystemFonts()

Matplotlib provide the function namely matplotlib.font_manager.findSystemFonts() which returns a list of paths to available fonts on the system.

Example

In this example we are using the matplotlib.font_manager.findSystemFonts() function to get the required indices list of font names.

import matplotlib.font_manager as fm
# Get a list of available fonts
font_list = fm.findSystemFonts()

# Display the first five fonts path
print("The first five fonts path:",font_list[:5])
Output
The first five fonts path: ['C:\\Windows\\Fonts\\gadugi.ttf', 'C:\\WINDOWS\\Fonts\\COPRGTB.TTF', 'C:\\WINDOWS\\Fonts\\KUNSTLER.TTF', 'C:\\Windows\\Fonts\\OLDENGL.TTF', 'C:\\Windows\\Fonts\\taileb.ttf']

Accessing Fonts by Indexing

Font indexing involves knowing the names or paths of available fonts on our system. We can refer these fonts by their names, aliases or file paths to set the font family in Matplotlib plots by ensuring that the desired font is used for text elements.

Example

Here in this example we are accessing the fonts from the system by using the font path.

import matplotlib as mpl
import matplotlib.font_manager as fm
plt.rcParams['font.family'] = 'C:\Windows\Fonts\Comicz.ttf'

# Creating a data
x = [i for i in range(10,40)]
y = [i for i in range(30,60)]

# Creating a plot
plt.plot(x,y,color = 'violet')
plt.xlabel('X-axis cube values')
plt.ylabel('Y-axis cube values')
plt.title("The plot fonts with font indexing")
plt.show()

Output

Font Indexing

Note

  • Font indexing and availability may vary depending on the backend being used in Matplotlib library.

  • Customizing fonts with indices might be backend-specific or require special configurations such as LaTeX rendering.

Advertisements