
- 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 - 矢羽圖
- Matplotlib 有用資源
- Matplotlib - 快速指南
- Matplotlib - 有用資源
- Matplotlib - 討論
Matplotlib - 全域性設定字型屬性
在 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']。這將把定義的外觀應用於整個繪圖中的文字元素。
示例
在這個示例中,我們將字型樣式指定為 italic 到 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
透過使用它,我們可以設定字型粗細,例如 'bold' 等。這使得文字元素中的字元根據使用者的要求以定義的樣式出現。
示例
在這個示例中,我們將字型粗細指定為粗體到 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()
輸出
