
- 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 Selector
- 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 庫中常見的可用基本單位。
磅 (pt)
在 Matplotlib 中,**磅**通常指的是用於指定繪圖中各種視覺屬性(例如線寬、標記大小和文字大小)的度量單位。磅 (pt) 是排版和圖形學中的標準單位,經常因其與裝置無關的大小表示而被使用。
磅在 Matplotlib 中的用法
**線寬** - Matplotlib 函式(例如 **plt.plot()**)中的 **linewidth** 引數允許我們以磅為單位指定線的寬度。
**文字大小** - Matplotlib 函式 **plt.xlabel()、plt.ylabel()、plt.title()** 中的 **fontsize** 引數允許設定文字大小(以磅為單位)。
**標記大小** - plot 函式(例如 plt.plot())中的 **markersize** 引數以磅為單位定義標記的大小。
**圖形大小** - 使用 **plt.figure(figsize=(width, height))** 指定圖形大小時,尺寸以英寸為單位。但是,預設大小在不同系統上可能有所不同,例如 **1** 英寸並不總是等於 **72** 磅,但這是一種典型的約定。
磅與其他單位的轉換
Matplotlib 在內部使用磅,但在設定圖形大小或尺寸時,通常使用英寸。作為參考,**1** 英寸大約等於 **72** 磅。
- 1 英寸 = 72 磅
- 1 磅 ≈ 0.01389 英寸
理解和利用磅作為 Matplotlib 中的度量單位,可以精確控制繪圖中元素的視覺屬性,確保所需的顯示效果和可讀性。
示例
在這個例子中,我們為線圖設定了以磅為單位的線寬。
import matplotlib.pyplot as plt plt.figure(figsize=(6, 4)) # 6 inches wide, 4 inches tall plt.plot([1, 2, 3], [4, 5, 6], linewidth=3.5) # Line width in points plt.xlabel('X-axis', fontsize=12) # Text size in points plt.ylabel('Y-axis', fontsize=12) plt.show()
輸出
以下是上述程式碼的輸出 -

英寸 (in)
英寸用於在 Matplotlib 中指定圖形或子圖的整體大小。例如,**figsize = (width, height)**(以英寸為單位)定義圖形的寬度和高度。
示例
在這個例子中,我們以英寸為單位指定了圖形的大小。
import matplotlib.pyplot as plt plt.figure(figsize=(4, 4)) # 6 inches wide, 4 inches tall plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Example Plot with 4x4 Inches') plt.show()
輸出
以下是上述程式碼的輸出 -

轉換
英寸與 Matplotlib 中常用的其他單位之間的轉換系數
1 英寸 ≈ 2.54 釐米
資料單位
資料單位表示沿 x 軸和 y 軸繪製的實際值。Matplotlib 使用這些單位將資料點繪製到圖表上。從資料單位到繪圖上物理單位的轉換由座標軸比例尺(例如線性、對數等)決定。
關於 Matplotlib 中資料單位的關鍵點
**數值資料** - 資料單位表示我們沿 x 軸和 y 軸繪製的數值。
**對映到繪圖空間** - Matplotlib 根據定義的座標軸限制和縮放比例,將這些數值資料值對映到繪圖空間中的特定位置。
**繪製資料** - 當我們使用 Matplotlib 繪製資料時,我們必須使用數值資料指定 x 和 y 座標。Matplotlib 會處理這些資料點在繪圖中的縮放和定位。
**座標軸限制** - 使用 **xlim** 設定 x 軸限制,使用 **ylim** 設定 y 軸限制,定義繪圖中顯示的資料單位範圍。
**座標軸刻度** - 座標軸上的刻度表示特定資料單位,為繪圖中存在的資料範圍提供視覺參考。
示例
在這個例子中,我們設定了繪圖的資料單位。
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Plotting using data units plt.plot(x, y, marker='o') # Setting axis limits in data units plt.xlim(0, 6) # X-axis limits from 0 to 6 plt.ylim(0, 12) # Y-axis limits from 0 to 12 plt.xlabel('X-axis (Data Units)') plt.ylabel('Y-axis (Data Units)') plt.title('Plot with Data Units') plt.show()
輸出
以下是上述程式碼的輸出 -

理解 Matplotlib 中的資料單位對於準確地視覺化資料並確保繪製的表示與底層數值正確對應至關重要。
座標軸座標
座標軸座標的範圍是從 0.0 到 1.0,它指定單個子圖或座標軸內的相對位置。**(0, 0)** 表示子圖的左下角,**(1, 1)** 表示子圖的右上角。
座標軸座標提供了一種靈活的方法,可以在子圖內相對於其大小和邊界來定位註釋、文字或其他元素,確保在不同繪圖大小或尺寸下的一致性和響應性定位。
示例
在這個例子中,我們設定了繪圖的座標軸座標 x 和 y。
import matplotlib.pyplot as plt # Creating a subplot fig, ax = plt.subplots() # Plotting data ax.plot([1, 2, 3], [2, 4, 3]) # Annotating at axes coordinates ax.annotate('Important Point', xy=(2, 4), # Data point to annotate xytext=(0.5, 0.5), # Position in axes coordinates textcoords='axes fraction', # Specifying axes coordinates arrowprops=dict(facecolor='black', arrowstyle='->')) plt.show()
輸出
以下是上述程式碼的輸出 -

圖形座標
圖形座標的範圍也是從 **0.0** 到 **1.0**,但是它相對於整個圖形畫布指定位置。**(0, 0)** 是整個圖形的左下角,**(1, 1)** 是整個圖形的右上角。
示例
這是一個設定繪圖圖形座標的示例。
import matplotlib.pyplot as plt # Creating subplots fig, (ax1, ax2) = plt.subplots(1, 2) # Plotting data in subplot 1 ax1.plot([1, 2, 3], [2, 4, 3]) # Plotting data in subplot 2 ax2.plot([1, 2, 3], [3, 1, 4]) # Annotating at figure coordinates fig.text(0.5, 0.9, 'Figure Annotation', ha='center', fontsize=12, color='red') plt.show()
輸出
以下是上述程式碼的輸出 -

使用 Matplotlib 繪製所有基本單位的圖形
此示例展示了在 Matplotlib 中使用各種單位,例如英寸表示圖形大小,磅表示文字大小,資料單位表示繪製資料點,以及座標軸座標表示在子圖中定位註釋。
示例
import matplotlib.pyplot as plt # Define a figure with specified size in inches fig = plt.figure(figsize=(6, 4)) # Width: 6 inches, Height: 4 inches # Create an axis within the figure ax = fig.add_subplot(111) # Plotting data points using data units x = [1, 2, 3, 4] y = [2, 4, 6, 8] ax.plot(x, y, marker='o') # Setting labels and titles using text in points ax.set_xlabel('X-axis Label', fontsize=12) ax.set_ylabel('Y-axis Label', fontsize=12) ax.set_title('Plot Title', fontsize=14) # Setting the position of a text annotation using axes coordinates ax.text(0.5, 0.5, 'Annotation', transform=ax.transAxes, fontsize=10) plt.show()
輸出
以下是上述程式碼的輸出 -
