Matplotlib 简明教程
Matplotlib - Font Indexing
在 Matplotlib 库中,字体索引是指访问和使用可用于在绘图中渲染文本元素的不同字体或字体的。Matplotlib 库提供了访问各种字体,并且理解字体索引涉及通过名称或索引知道如何使用这些字体。当我们要使用不在默认集合中的字体或需要使用特定于系统的字体时,这一点尤为重要。
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.
Matplotlib 中的 font.family 参数接受一个字符串(表示已知的字体系列)或一个整数(表示特定的字体索引)。数字索引用于引用已在 Matplotlib 库中注册的字体。
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
以下是 matplotlib 库中的字体索引方法。
The following are the font indexing ways in matplotlib library.
Accessing Fonts by Name
为了在 Matplotlib 中访问字体,我们可以使用字体名称及其索引(如果可用)。
To access fonts in Matplotlib we can use both the font names and their indices if available.
Font Names
我们可以使用字体名称访问 matplotlib 中可用的字体。以下是使用 plt.rcParams['font.family'] 方法访问字体 'Times New Roman' 的示例。
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.
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()
Available Fonts on the System
Matplotlib 库还可以使用我们系统上的可用字体。可用的字体可能根据操作系统(例如 Windows、macOS、Linux)和已安装的字体库而异。
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 提供了函数 matplotlib.font_manager.findSystemFonts() ,它返回系统上可用字体的路径列表。
Matplotlib provide the function namely matplotlib.font_manager.findSystemFonts() which returns a list of paths to available fonts on the system.
在此示例中,我们使用 matplotlib.font_manager.findSystemFonts() 函数来获取字体名称所需索引列表。
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])
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
字体索引涉及了解其系统上的可用字体的名称或路径。通过确保将所需字体用于文本元素,我们可以根据其名称、别名或文件路径来引用这些字体以在 Matplotlib 绘图中设置字体系列。
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()