Matplotlib - 字型是什麼?



在 Matplotlib 庫中,“字型”指的是用於渲染繪圖和視覺化中文字的字型型別。字型在自定義文字元素(例如標籤、標題、註釋和圖例)的外觀方面起著重要作用。

Matplotlib 庫中字型的關鍵方面

  • 字體系列 - 指字型的樣式或類別。常見的字體系列包括襯線、非襯線、等寬等。每個系列都有其自身的視覺特徵。

  • 字型樣式 - 確定文字的外觀,例如正常、斜體或傾斜。

  • 字型粗細 - 指定字型的粗細或粗體程度,從正常到不同級別的粗體。

在 Matplotlib 中控制字型

  • 設定字型屬性 - 我們可以使用諸如`fontsize`、`fontstyle`、`fontweight``fontfamily`等引數來控制繪圖中文字元素的字型屬性,這些引數在諸如`plt.xlabel()`、`plt.title()`等函式中使用。

plt.xlabel('X-axis Label', fontsize=12, fontstyle='italic', fontweight='bold', fontfamily='serif')
  • 全域性字型配置 - 使用`plt.rcParams`全域性調整整個繪圖的字型屬性,使我們能夠設定預設字型設定以保持一致性。

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

字型在視覺化中的重要性

  • 可讀性 - 字型的選擇會顯著影響繪圖中文字元素的可讀性。選擇合適的字型可以提高視覺化資訊的清晰度。

  • 美觀性 - 字型透過影響其視覺吸引力和呈現方式來提升繪圖的整體美觀性。

  • 強調和風格 - 不同的字型傳達不同的語氣和風格,允許使用者強調特定元素或建立特定的視覺氛圍。

全域性設定字型屬性

我們可以使用 plt.rcParams 全域性配置整個繪圖的字型屬性。

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

在接下來的章節中,讓我們詳細瞭解每個字型引數。

Matplotlib 中常見的與字型相關的函式

以下是 matplotlib 庫中常見的與字型相關的函式。

plt.rcParams

在 Matplotlib 中,plt.rcParams 是一個類似字典的物件,允許您全域性配置影響繪圖和圖形外觀和行為的各種設定。它充當 Matplotlib 的中央配置系統,提供了一種便捷的方式來設定視覺化中不同元素的預設引數。

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

這些函式用於設定軸標籤和標題的字型屬性。

plt.text(), plt.annotate()

這些函式用於指定註釋和文字元素的字型屬性。

獲取當前可用的所有字型列表

要獲取 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

獲取字體系列(或字型名稱)列表

在這裡,透過使用以下程式碼,我們將獲得字體系列列表,即字型名稱

示例

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
廣告