Matplotlib 简明教程

Matplotlib - What are Fonts?

在 Matplotlib 库中, ‘Fonts’ 引用用于在绘图和可视化中呈现文本的字体。字体在自定义标签、标题、注释和绘图中图例等文本元素的外观方面发挥着重要作用。

Key Aspects of Fonts in Matplotlib library

  1. Font Family - 指字体的样式或类别。常见的字体系列包括衬线、非衬线、等宽等。每个系列都有其自身的视觉特征。

  2. Font Style - 确定文本的外观,例如普通、斜体或倾斜。

  3. Font Weight - 指定字体的粗细或粗细程度,从普通到各种粗细级别。

Controlling Fonts in Matplotlib

  1. Setting Font Properties - 我们可以使用 fontsize, fontstyle, fontweightfontfamily 等参数在 plt.xlabel(), plt.title() 等函数中控制绘图中文本元素的字体属性。

plt.xlabel('X-axis Label', fontsize=12, fontstyle='italic', fontweight='bold', fontfamily='serif')
  1. Global Font Configuration − 使用 plt.rcParams 为整个绘图全局调整字体属性,以便我们为一致性设置默认字体设置。

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

Importance of Fonts in Visualization

  1. Readability − 字体选择对绘图中文本元素的可读性有重大影响。选择适当的字体可提高可视化信息的清晰度。

  2. Aesthetics − 字体通过影响绘图的视觉吸引力和呈现方式来促成绘图的整体美感。

  3. Emphasis and Style − 不同的字体传达不同的语调和风格,让用户可以强调特定元素或营造特定的视觉氛围。

Setting Font Properties Globally

我们可以使用 plt.rcParams 为整个绘图全局配置字体属性。

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

在即将到来的章节中,我们来分别仔细了解字体的每个参数。

以下是 matplotlib 库中常见的字体相关函数。

plt.rcParams

Matplotlib 中的 plt.rcParams 是一个类似字典的对象,允许您全局配置影响绘图和图形的外观和行为的各种设置。它用作 Matplotlib 的一个中心配置系统,提供了一种方便的方法,用于为可视化中不同的元素设置默认参数。

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

这些函数用于设置坐标轴标签和标题的字体属性。

plt.text(), plt.annotate()

这些函数用于指定注释和文本元素的字体属性。

Get a list of all the fonts currently available

要获取 matplotlib 当前所有可用的字体列表,我们可以使用 font_manager.findSystemFonts() 方法。

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)

这里通过使用以下代码,我们可以获取字体系列(即字体名称)的列表。

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