
- 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 - Span 選擇器
- 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 中的關鍵字型屬性
字體系列
字體系列指定用於文字元素的字型型別。常見的字體系列包括 serif、sans-serif、monospace 等。
- serif - 帶裝飾筆劃的字型,通常用於更傳統或正式的外觀。
- sans-serif - 不帶裝飾筆劃的字型,以其簡潔現代的外觀而聞名,通常用於提高可讀性。
- monospace - 每個字元佔用相同水平空間的字型,通常用於程式碼或表格資料。
- 自定義或特定字型 - 使用者也可以使用安裝在其系統上的自定義字型,或提供特定字型的字型路徑。
字型大小
字型大小決定文字的大小(以磅為單位),影響可讀性和可見性。字型大小以磅 (pt) 為單位指定,其中 1 磅約為 1/72 英寸。Matplotlib 使用磅作為字型大小的標準單位,以便在不同的裝置和顯示解析度下保持一致性。
字型粗細
字型粗細控制文字的厚度或粗細。選項範圍從正常到粗體。它允許使用者控制文字元素(如標籤、標題、註釋和其他文字元件)的視覺重點。
字型粗細選項
- normal - 指定正常字型粗細。
- bold - 指定粗體字型粗細。
- 整數值 - 一些字型支援 100 到 900 的數值範圍來指定粗細級別。
字型樣式
字型樣式指定文字的樣式,例如正常、斜體或傾斜。它允許使用者控制文字元素(如標籤、標題、註釋和其他文字元件)的傾斜度或樣式。
字型樣式選項
- normal - 指定正常字型樣式(無傾斜或斜體)。
- italic - 指定斜體字型樣式,使字元傾斜。
- oblique - 與斜體類似,但在某些字型中外觀可能略有不同。
在 Matplotlib 中設定字型屬性
以下是設定 matplotlib 中字型屬性的方法。
全域性配置 (使用 plt.rcParams)
這用於為繪圖或圖形中的所有文字元素配置預設字型屬性。
示例
import matplotlib.pyplot as plt # Set global font properties x = [2,3,4,6] y = [9,2,4,7] plt.rcParams['font.family'] = 'sans-serif' plt.rcParams['font.size'] = 8 plt.rcParams['font.weight'] = 'normal' plt.rcParams['font.style'] = 'italic' plt.plot(x,y) plt.xlabel("x-axis") plt.ylabel("y-axis") plt.title("Setting fonts globally") plt.show()
輸出

單個文字元素
透過這種方法,我們可以為繪圖中的特定文字元素設定字型屬性。
示例
import matplotlib.pyplot as plt # Set Individual font properties x = [2,3,4,6] y = [9,2,4,7] plt.plot(x,y) plt.xlabel('X-axis Label', fontsize=14, fontweight='bold', fontstyle='italic') plt.ylabel('Y-axis Label', fontsize=14, fontweight='bold', fontstyle='normal') plt.title("Setting fonts Individually") plt.show()
輸出

字型屬性的重要性
以下是字型屬性的重要性。
可讀性
正確的字型選擇和大小可以增強文字元素的可讀性。
美觀
字型樣式和系列有助於提高繪圖的視覺吸引力。
溝通
字型屬性有助於在視覺化中傳達重點或上下文。
使用字型屬性進行自定義
- 調整字型屬性允許使用者調整文字外觀以匹配視覺化或簡報的要求。
- 一致且適當的字型屬性使用確保在繪圖和圖形中以視覺上連貫且資訊豐富的方式顯示文字資訊。
最後,我們可以說 Matplotlib 庫中的字型屬性為使用者提供了自定義文字外觀的靈活性,確保視覺化中的清晰度、可讀性和視覺吸引力。這些屬效能夠精確控制繪圖中文字元素的顯示方式,從而有效地將資訊傳達給觀眾。
一個標籤中的多個字型大小
在這個示例中,我們在 Python 中使用 title() 方法的 fontsize 引數在一個標籤中使用多個字型大小。
示例
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(-5, 5, 100) y = np.cos(x) plt.plot(x, y) fontsize = 20 plt.title("$bf{y=cos(x)}$", fontsize=fontsize) plt.axis('off') plt.show()
輸出

更改圖例中字型的文字顏色
在這個示例中,我們將更改圖例中字型的文字顏色。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 100) y = np.exp(x) plt.plot(x, y, label="y=exp(x)", c='red') leg = plt.legend(loc='upper left') for text in leg.get_texts(): text.set_color("green") plt.show()
輸出

更改所有文字的預設字型顏色
在這個示例中,我們將更改繪圖中所有文字的預設字型顏色。
示例
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True print("Default text color is: ", plt.rcParams['text.color']) plt.rcParams.update({'text.color': "red", 'axes.labelcolor': "green"}) plt.title("Title") plt.xlabel("X-axis") plt.show()
輸出
預設文字顏色為:黑色

更改座標軸物件的刻度字型大小
在這個示例中,我們將更改繪圖中所有文字的預設字型顏色。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 10) y = np.sin(x) fig, ax = plt.subplots() ax.plot(x, y, c='red', lw=5) ax.set_xticks(x) for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(14) tick.label.set_rotation('45') plt.tight_layout() plt.show()
輸出

增加Seaborn繪圖圖例的字型大小
在這個示例中,我們增加了Seaborn繪圖中圖例的字型大小,我們可以使用fontsize變數,並將其用於legend()方法的引數中。
示例
import pandas import matplotlib.pylab as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pandas.DataFrame(dict( number=[2, 5, 1, 6, 3], count=[56, 21, 34, 36, 12], select=[29, 13, 17, 21, 8] )) bar_plot1 = sns.barplot(x='number', y='count', data=df, label="count", color="red") bar_plot2 = sns.barplot(x='number', y='select', data=df, label="select", color="green") fontsize = 20 plt.legend(loc="upper right", frameon=True, fontsize=fontsize) plt.show()
輸出
