
- Matplotlib 基礎
- Matplotlib - 首頁
- Matplotlib - 簡介
- Matplotlib - 與 Seaborn 的比較
- Matplotlib - 環境設定
- Matplotlib - Anaconda 發行版
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - 簡單繪圖
- Matplotlib - 儲存圖表
- Matplotlib - 標記
- Matplotlib - 圖表
- Matplotlib - 風格
- Matplotlib - 圖例
- Matplotlib - 顏色
- Matplotlib - 顏色圖
- Matplotlib - 顏色圖歸一化
- Matplotlib - 選擇顏色圖
- Matplotlib - 顏色條
- Matplotlib - 文字
- Matplotlib - 文字屬性
- Matplotlib - 子圖示題
- Matplotlib - 圖片
- Matplotlib - 圖片蒙版
- Matplotlib - 註釋
- Matplotlib - 箭頭
- Matplotlib - 字型
- Matplotlib - 什麼是字型?
- 全域性設定字型屬性
- Matplotlib - 字型索引
- Matplotlib - 字型屬性
- Matplotlib - 比例尺
- Matplotlib - 線性和對數比例尺
- Matplotlib - 對稱對數和 Logit 比例尺
- Matplotlib - LaTeX
- Matplotlib - 什麼是 LaTeX?
- Matplotlib - 用於數學表示式的 LaTeX
- Matplotlib - 註釋中的 LaTeX 文字格式
- Matplotlib - PostScript
- 啟用註釋中的 LaTeX 渲染
- Matplotlib - 數學表示式
- Matplotlib - 動畫
- Matplotlib - 圖元
- Matplotlib - 使用 Cycler 進行樣式設定
- Matplotlib - 路徑
- Matplotlib - 路徑效果
- Matplotlib - 變換
- Matplotlib - 刻度和刻度標籤
- Matplotlib - 弧度刻度
- Matplotlib - 日期刻度
- Matplotlib - 刻度格式化器
- Matplotlib - 刻度定位器
- Matplotlib - 基本單位
- Matplotlib - 自動縮放
- Matplotlib - 反轉座標軸
- Matplotlib - 對數座標軸
- Matplotlib - Symlog
- Matplotlib - 單位處理
- Matplotlib - 帶有單位的橢圓
- Matplotlib - 脊柱
- Matplotlib - 座標軸範圍
- Matplotlib - 座標軸比例尺
- Matplotlib - 座標軸刻度
- Matplotlib - 格式化座標軸
- Matplotlib - Axes 類
- Matplotlib - 雙座標軸
- Matplotlib - Figure 類
- Matplotlib - 多圖
- Matplotlib - 網格
- Matplotlib - 面向物件介面
- Matplotlib - PyLab 模組
- Matplotlib - Subplots() 函式
- Matplotlib - Subplot2grid() 函式
- Matplotlib - 錨定圖元
- Matplotlib - 手動等高線
- Matplotlib - 座標報告
- Matplotlib - AGG 濾鏡
- Matplotlib - 緞帶框
- Matplotlib - 填充螺旋線
- Matplotlib - Findobj 演示
- Matplotlib - 超連結
- Matplotlib - 圖片縮圖
- Matplotlib - 使用關鍵字繪圖
- Matplotlib - 建立 Logo
- Matplotlib - 多頁 PDF
- Matplotlib - 多程序
- Matplotlib - 列印標準輸出
- Matplotlib - 複合路徑
- Matplotlib - Sankey 類
- Matplotlib - MRI 與 EEG
- Matplotlib - 樣式表
- Matplotlib - 背景顏色
- Matplotlib - Basemap
- Matplotlib 事件處理
- Matplotlib - 事件處理
- Matplotlib - 關閉事件
- Matplotlib - 滑鼠移動
- Matplotlib - 點選事件
- Matplotlib - 滾動事件
- Matplotlib - 按鍵事件
- Matplotlib - 選擇事件
- Matplotlib - 透視鏡
- Matplotlib - 路徑編輯器
- Matplotlib - 多邊形編輯器
- Matplotlib - 定時器
- Matplotlib - Viewlims
- Matplotlib - 縮放視窗
- Matplotlib 小部件
- Matplotlib - 游標小部件
- Matplotlib - 帶註釋的游標
- Matplotlib - 按鈕小部件
- Matplotlib - 複選框
- Matplotlib - 套索選擇器
- Matplotlib - 選單小部件
- Matplotlib - 滑鼠游標
- Matplotlib - 多游標
- Matplotlib - 多邊形選擇器
- Matplotlib - 單選按鈕
- Matplotlib - RangeSlider
- Matplotlib - 矩形選擇器
- Matplotlib - 橢圓選擇器
- Matplotlib - 滑塊小部件
- Matplotlib - 區間選擇器
- Matplotlib - 文字框
- Matplotlib 繪圖
- Matplotlib - 條形圖
- Matplotlib - 直方圖
- Matplotlib - 餅圖
- Matplotlib - 散點圖
- Matplotlib - 箱線圖
- Matplotlib - 小提琴圖
- Matplotlib - 等高線圖
- Matplotlib - 3D 繪圖
- Matplotlib - 3D 等高線
- Matplotlib - 3D 線框圖
- Matplotlib - 3D 表面圖
- Matplotlib - Quiver 圖
- Matplotlib 有用資源
- Matplotlib - 快速指南
- Matplotlib - 有用資源
- Matplotlib - 討論
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.rcParam - 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
控制字型屬性
字型大小 (`fontsize`)
使用諸如plt.xlabel()、plt.ylabel()、plt.title()等函式中的fontsize引數設定文字元素的大小。
示例
在這個示例中,我們透過將fontsize引數傳遞給plt.xlabel()、plt.ylabel()和plt.title()函式來設定文字的大小。
import matplotlib.pyplot as plt # 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', fontsize = 10) plt.ylabel('Y-axis cube values', fontsize = 12) plt.title('plot with defined font size') plt.show()
輸出

字型樣式 (`fontstyle`)
我們可以為要在建立的繪圖上使用的字型選擇字型樣式,例如“normal”、“italic”或“oblique”。
示例
在這裡,我們透過將 fontstyle 引數作為'normal'、'italic'或'oblique'傳遞給plt.xlabel()、plt.ylabel()和plt.title()來設定我們在繪圖上顯示的字型的字型樣式。
import matplotlib.pyplot as plt # 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', fontsize = 10) plt.ylabel('Y-axis cube values', fontsize = 12) plt.title('plot with defined font size') plt.show()
輸出

字型粗細 (`fontweight`)
使用諸如'normal'、'bold'或數值(例如400、700等)調整字型的粗細。
示例
import matplotlib.pyplot as plt # 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', fontweight='bold') plt.ylabel('Y-axis cube values', fontweight='bold') plt.title('plot with defined font size',fontweight='bold') plt.show()
輸出

更改字體系列
我們可以修改 Matplotlib 庫中各種文字元素的字體系列。下面提到一些可用的字體系列。
'serif' - 襯線字型,例如 Times New Roman
'sans-serif' - 非襯線字型,例如 Arial
'monospace' - 等寬字型,例如 Courier New
更改字型大小
我們可以使用 plt.text() 函式更改字型大小。
示例
import matplotlib.pyplot as plt plt.plot([1, 2, 4], [1, 2, 4]) plt.text(2, 3, "y=x", color='red', fontsize=20) # Increase fontsize by increasing value. plt.show()
輸出

將字型嵌入 Matplotlib 生成的 PDF 中
要將字型嵌入 Matplotlib 生成的 PDF 中,我們可以使用 rc.Params['pdf.fonttype']=42。
示例
import numpy as np from matplotlib import pyplot as plt, font_manager as fm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.rcParams['pdf.fonttype'] = 42 fig, ax = plt.subplots() x = np.random.rand(100) y = np.random.rand(100) ax.scatter(x, y, c=y, marker="v") fprop = fm.FontProperties(fname='C:\Windows\Fonts\MISTRAL.TTF') ax.set_title('Scatter Plot With Random Points', fontproperties=fprop, size=20, fontweight="bold") plt.savefig("demo.pdf")
輸出
當我們執行程式碼時,它將以“demo.pdf”作為檔名將以下繪圖儲存在專案目錄中。

全域性設定字型屬性
在 Matplotlib 庫中全域性設定字型屬性涉及使用plt.rcParams方法配置整個繪圖的預設字型設定。當我們想要將一致的字型樣式、大小或系列應用於視覺化效果中的所有文字元素而無需為每個元件分別指定它們時,這非常有用。
重要的是要注意,我們可以根據我們的喜好和我們想要在繪圖中實現的視覺樣式來定製這些設定。我們可以調整值以匹配我們所需的字型樣式、大小和系列,以確保視覺化效果的一致性和美觀性。
以下是我們可以使用plt.rcParams方法進行的不同設定。
plt.rcParams['font.family'] = font_name
這將設定文字元素的預設字體系列,例如'sans-serif'。我們可以將 font_name 替換為可用的字體系列,例如'serif'、'monospace'或特定字型名稱。
示例
在這個示例中,我們嘗試使用plt.rcParams['font.family']將字體系列設定為'sans-serif'。
import matplotlib.pyplot as plt # Set font properties globally plt.rcParams['font.family'] = 'sans-serif' # Create a plot plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label') plt.title('Font Family setting') plt.show()
輸出

plt.rcParams['font.size'] = font_size
我們可以根據需要以數值指定文字元素的預設字型大小。這確保了所有文字元素都使用此字型大小,除非為特定元件覆蓋。
示例
在這個例子中,我們將字型大小指定為8磅,用於plt.rcParams['font.size'],以便在整個繪圖中全域性顯示字型大小。
import matplotlib.pyplot as plt # Set font properties globally plt.rcParams['font.size'] = 8 # Create a plot plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label') plt.title('Font Size setting') plt.show()
輸出

plt.rcParams['font.style'] = 'fontstyle'
我們可以根據自己的興趣,定義字型樣式,例如斜體等,用於plt.rcParams['font.style']。這會將定義的外觀應用於整個繪圖中的文字元素。
示例
在這個例子中,我們將字型樣式指定為斜體,用於plt.rcParams['font.style']。
import matplotlib.pyplot as plt # Set font properties globally plt.rcParams['font.style'] = 'italic' # Create a plot plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label') plt.title('Font Style setting') plt.show()
輸出

plt.rcParams['font.weight'] = font_weight
透過使用這個,我們可以設定字型粗細,例如'粗體'等。這使得文字元素中的字元根據使用者需求以定義的樣式顯示。
示例
在這個例子中,我們將字型粗細指定為粗體,用於plt.rcParams['font.weight']。
import matplotlib.pyplot as plt # Set font properties globally plt.rcParams['font.weight'] = 'bold' # Create a plot plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label') plt.title('Font weight setting') plt.show()
輸出

注意 − 透過使用plt.rcParams全域性設定這些引數,我們可以確保這些預設字型屬性應用於所有Matplotlib繪圖中的文字元素。當我們建立標籤、標題或其他文字元件時,它們將繼承這些全域性設定,除非我們在本地指定不同的屬性。
更改Matplotlib顏色條標籤的字型屬性
在這個例子中,我們正在更改matplotlib顏色條標籤的字型屬性。
示例
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x, y = np.mgrid[-1:1:100j, -1:1:100j] z = (x + y) * np.exp(-5.0 * (x ** 2 + y ** 2)) plt.imshow(z, extent=[-1, 1, -1, 1]) cb = plt.colorbar(label='my color bar label') plt.show()
輸出

設定繪圖畫布的大小
在這個例子中,我們正在設定matplotlib中繪圖畫布的大小。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 100) y = np.sin(x) plt.plot(x, y) plt.show()
輸出

使用Matplotlib自動調整Seaborn熱力圖中的字型大小
在這裡,我們使用Matplotlib庫自動調整seaborn熱力圖中的字型大小。
示例
import numpy as np import seaborn as sns from matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True d = { 'y=1/x': [1 / i for i in range(1, 10)], 'y=x': [i for i in range(1, 10)], 'y=x^2': [i * i for i in range(1, 10)], 'y=x^3': [i * i * i for i in range(1, 10)] } df = pd.DataFrame(d) ax = sns.heatmap(df, vmax=1) plt.xlabel('Mathematical Expression', fontsize=16) plt.show()
輸出

設定Matplotlib軸圖例的字型大小
在這個例子中,我們正在設定matplotlib軸圖例的字型大小。
示例
import numpy as np from matplotlib import pyplot as plt import matplotlib plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 50) y = np.sin(x) plt.plot(x, y, c="red", lw=7, label="y=sin(x)") plt.title("Sine Curve") matplotlib.rcParams['legend.fontsize'] = 20 plt.legend(loc=1) plt.show()
輸出

修改Matplotlib-venn中的字型大小
要使用Matplotlib-venn,首先我們必須使用下面的程式碼安裝包。如果之前已經安裝,我們可以直接匯入。
pip install matplotlib-venn
示例
在這個例子中,我們使用set_fontsize()方法修改Matplotlib-venn中的字型大小。
from matplotlib import pyplot as plt from matplotlib_venn import venn3 plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True set1 = {'a', 'b', 'c', 'd'} set2 = {'a', 'b', 'e'} set3 = {'a', 'd', 'f'} out = venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3')) for text in out.set_labels: text.set_fontsize(25) for text in out.subset_labels: text.set_fontsize(12) plt.show()
輸出

更改xticks字型大小
要更改matplotlib繪圖中xticks的字型大小,我們可以使用fontsize引數。
示例
from matplotlib import pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # x and y data points x = np.linspace(-5, 5, 100) y = np.sin(x) plt.plot(x, y) # Set the font size of xticks plt.xticks(fontsize=25) # Display the plot plt.show()
輸出
