Matplotlib 简明教程
Matplotlib - What are Fonts?
在 Matplotlib 库中, ‘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
andfontfamily
in the functions such asplt.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
我们可以使用 plt.rcParams 为整个绘图全局配置字体属性。
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
以下是 matplotlib 库中常见的字体相关函数。
The following are the common font related functions in matplotlib library.
plt.rcParams
Matplotlib 中的 plt.rcParams 是一个类似字典的对象,允许您全局配置影响绘图和图形的外观和行为的各种设置。它用作 Matplotlib 的一个中心配置系统,提供了一种方便的方法,用于为可视化中不同的元素设置默认参数。
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
要获取 matplotlib 当前所有可用的字体列表,我们可以使用 font_manager.findSystemFonts() 方法。
To get a list of all the fonts currently available for matplotlib we can use the font_manager.findSystemFonts() method.
from matplotlib import font_manager
print("List of all fonts currently available in the matplotlib:")
print(*font_manager.findSystemFonts(fontpaths=None, fontext='ttf'), sep="")
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
from matplotlib import font_manager
print("List of all fonts currently available in the matplotlib:")
print(*font_manager.findSystemFonts(fontpaths=None, fontext='ttf'), sep="")
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