pythonsubplots:用 Matplotlib 創建多個子圖

Matplotlib 是一個用於繪製圖形的 Python 2D 繪圖庫,可以創建各種靜態、動態、互動式圖表。其中,pythonsubplots 功能是 Matplotlib 中非常有用的一個子圖工具,可以在一個圖中創建多個子圖,對於多個數據的比較和展示非常方便。本文將詳細介紹 pythonsubplots 的用法。

一、創建多個子圖

在 Matplotlib 中,使用 Python 腳本可以自動生成各種用於數據可視化的圖表,而我們用 pythonsubplots 時可以將一張大圖分割成多個小圖。代碼示例如下:

import matplotlib.pyplot as plt

# 創建一個 2x2 的圖表結構,分別放置2個子圖
fig, axs = plt.subplots(2, 2)

# 第一個子圖
axs[0, 0].plot([1, 2], [3, 4])
axs[0, 0].set_title('Subplot 1')

# 第二個子圖
axs[0, 1].scatter([1, 2], [3, 4], color='r')
axs[0, 1].set_title('Subplot 2')

# 第三個子圖
axs[1, 0].bar([1, 2], [3, 4], color='g')
axs[1, 0].set_title('Subplot 3')

# 第四個子圖
axs[1, 1].pie([1, 2], colors=['b', 'y'])
axs[1, 1].set_title('Subplot 4')

plt.show()

以上代碼中,首先使用 plt.subplots(…) 創建一個 2×2 的子圖結構。其中 axs 為一個數組,通過 axs[row_index, col_index] 的方式進行訪問。

對於每個子圖,我們可以通過 axs[row_index, col_index].plot/scatter/bar/pie 等方法繪製具體的數據圖表,並使用 axs[row_index, col_index].set_title(…) 方法設置每個子圖的標題。最後,使用 plt.show() 方法展示所有子圖。

二、調整子圖間距和大小

在實際使用中,我們還需要對子圖之間的間距和大小進行調節。Matplotlib 提供了通過 subplot_params 參數調整子圖間距和 fig_kw/ax_kw 參數調整圖表大小的方法。具體示例如下:

import matplotlib.pyplot as plt

# 調整子圖間距和圖表大小
params = {
    'figure.figsize': [10, 6],   # 圖表大小
    'figure.subplot.left': 0.05,  # 子圖左間距
    'figure.subplot.right': 0.95,  # 子圖右間距
    'figure.subplot.bottom': 0.05,  # 子圖下間距
    'figure.subplot.top': 0.95,  # 子圖上間距
    'figure.subplot.wspace': 0.2,  # 子圖水平間距
    'figure.subplot.hspace': 0.3,  # 子圖垂直間距
}
plt.rcParams.update(params)

# 創建 2 x 2 的圖表結構
fig, axs = plt.subplots(2, 2)

# 第一個子圖
axs[0, 0].plot([1, 2], [3, 4])
axs[0, 0].set_title('Subplot 1')

# 第二個子圖
axs[0, 1].scatter([1, 2], [3, 4], color='r')
axs[0, 1].set_title('Subplot 2')

# 第三個子圖
axs[1, 0].bar([1, 2], [3, 4], color='g')
axs[1, 0].set_title('Subplot 3')

# 第四個子圖
axs[1, 1].pie([1, 2], colors=['b', 'y'])
axs[1, 1].set_title('Subplot 4')

plt.show()

以上代碼中,我們通過創建一個字典,將修改參數值並傳遞給 plt.rcParams.update(params) 方法實現調整。其中,參數較多,需要根據自己的實際需求進行各項參數設置。

三、添加大標題和標籤

Matplotlib 還提供了在多個子圖中添加大標題和標籤的方法,使圖表更有可讀性。代碼示例如下:

import matplotlib.pyplot as plt

# 創建 2 x 2 的圖表結構
fig, axs = plt.subplots(2, 2)

# 第一個子圖
axs[0, 0].plot([1, 2], [3, 4])
axs[0, 0].set_title('Subplot 1')

# 第二個子圖
axs[0, 1].scatter([1, 2], [3, 4], color='r')
axs[0, 1].set_title('Subplot 2')

# 第三個子圖
axs[1, 0].bar([1, 2], [3, 4], color='g')
axs[1, 0].set_title('Subplot 3')

# 第四個子圖
axs[1, 1].pie([1, 2], colors=['b', 'y'])
axs[1, 1].set_title('Subplot 4')

# 添加大標題
fig.suptitle('Title for All Subplots', fontsize=18)

# 添加標籤
fig.text(0.5, 0.04, 'X-axis Label', ha='center', fontsize=14)
fig.text(0.06, 0.5, 'Y-axis Label', va='center', rotation='vertical', fontsize=14)

plt.show()

以上代碼中,我們使用 fig.suptitle(…) 方法添加大標題,並使用 fig.text(…) 方法添加標籤。其中 fig.text(…) 的第一個參數為標籤的位置,以圖表左上角為原點,0.5 表示 x 軸的中間位置,0.04 表示 y 軸標籤位置。

四、限制子圖的坐標軸範圍

有時候,我們需要控制不同子圖之間的坐標軸範圍,從而更好地呈現不同的數據結構和特徵。代碼示例如下:

import matplotlib.pyplot as plt

# 創建 2 x 2 的圖表結構
fig, axs = plt.subplots(2, 2)

# 第一個子圖:限制橫坐標範圍和縱坐標範圍
axs[0, 0].plot([1, 2], [3, 4])
axs[0, 0].set_xlim([0, 3])
axs[0, 0].set_ylim([2, 5])
axs[0, 0].set_title('Subplot 1')

# 第二個子圖:限制橫縱坐標範圍
axs[0, 1].scatter([1, 2], [3, 4], color='r')
axs[0, 1].set_xlim([0, 3])
axs[0, 1].set_ylim([2, 5])
axs[0, 1].set_title('Subplot 2')

# 第三個子圖:限制縱坐標範圍
axs[1, 0].bar([1, 2], [3, 4], color='g')
axs[1, 0].set_ylim([0, 5])
axs[1, 0].set_title('Subplot 3')

# 第四個子圖:限制橫坐標範圍
axs[1, 1].pie([1, 2], colors=['b', 'y'])
axs[1, 1].set_xlim([0, 3])
axs[1, 1].set_title('Subplot 4')

plt.show()

以上代碼中,我們使用 axs[x_index, y_index].set_xlim([min_value, max_value]) 和 axs[x_index, y_index].set_ylim([min_value, max_value]) 方法實現對子圖坐標軸範圍的限制。其中 [min_value, max_value] 表示對坐標軸範圍的限制。

五、結論

通過以上示例,我們可以看到 pythonsubplots 在 Matplotlib 中的強大功能。pythonsubplots 可以使我們在一個圖表中展示多個數據,並通過調節子圖間距和大小,添加大標題和標籤,限制子圖的坐標軸範圍等方法,實現更好的數據呈現和分析。

原創文章,作者:FPHQL,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/313390.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
FPHQL的頭像FPHQL
上一篇 2025-01-07 09:43
下一篇 2025-01-07 09:43

相關推薦

發表回復

登錄後才能評論