Matplotlib - 脊柱



什麼是脊柱?

在 Matplotlib 庫中,脊柱指的是圍繞資料區域的繪圖邊框或邊緣。這些脊柱包含繪圖的邊界,定義了顯示資料點的區域。預設情況下,繪圖有四個脊柱,例如頂部、底部、左側和右側。

在 Matplotlib 中操作脊柱提供了設計繪圖視覺方面的靈活性,允許對資料的呈現進行更量身定製和美觀的呈現。

脊柱的關鍵特徵

以下是脊柱的特徵。

繪圖的邊界 - 脊柱構成繪圖區域的邊界,包圍了資料視覺化的區域。

可配置屬性 - 每個脊柱(頂部、底部、左側和右側)都可以單獨自定義,允許調整其外觀、顏色、粗細和可見性。

可見性控制 - 可以使脊柱可見或隱藏以修改繪圖的外觀。

脊柱的用途

繪圖自定義 - 脊柱允許自定義繪圖的外觀,可以調整繪圖的邊界和樣式。

美觀和視覺化 - 自定義脊柱可以增強繪圖的美觀性,並吸引人們注意感興趣的特定區域。

脊柱型別

現在讓我們詳細瞭解繪圖中每個脊柱。

頂部脊柱

頂部脊柱指的是繪圖區域頂部的水平線,對應於 y 軸的上邊界。它是構成繪圖周圍邊框的四個脊柱(頂部、底部、左側和右側)之一。

頂部脊柱的特徵

邊界線 - 頂部脊柱表示沿 y 軸的繪圖區域的上邊界。

預設可見性 - 預設情況下,頂部脊柱在 Matplotlib 繪圖中可見。

自定義 - 與其他脊柱類似,頂部脊柱可以在其可見性、顏色、線型和線寬方面進行自定義。

示例

在此示例中,ax.spines['top'].set_visible(False) 透過移除沿 y 軸的繪圖區域的上邊界來隱藏頂部脊柱。

import matplotlib.pyplot as plt

# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)

# Accessing and modifying the top spine
ax = plt.gca()  # Get the current axes
ax.spines['top'].set_visible(False)  # Hide the top spine
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Hidden Top Spine')
plt.show()
輸出
Top Spine

修改頂部脊柱的用例

美觀控制 - 自定義頂部脊柱的可見性、顏色或樣式可以改善外觀或匹配特定設計要求。

調整繪圖邊界 - 當繪圖不需要上邊界或建立特定視覺效果時,隱藏頂部脊柱可能很有用。

底部脊柱

在 Matplotlib 中,底部脊柱指的是構成繪圖區域底部邊框的水平線,對應於 x 軸。

底部脊柱的特徵

與 x 軸關聯 - 底部脊柱表示沿 x 軸的繪圖邊界,定義了繪圖區域的下邊界。

自定義 - 與其他脊柱類似,底部脊柱可以在其可見性、顏色、線型、粗細和位置方面進行自定義。

自定義底部脊柱的示例

在此示例中,使用ax.spines['bottom'].set_color('blue') 將底部脊柱的顏色更改為藍色,ax.spines['bottom'].set_linewidth(2) 將底部脊柱的粗細設定為 2,並且ax.spines['bottom'].set_visible(True) 確保底部脊柱可見(如果它被隱藏了)。

import matplotlib.pyplot as plt

# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)

# Accessing and customizing the bottom spine
ax = plt.gca()  # Get the current axes
ax.spines['bottom'].set_color('blue')  # Change the color of the bottom spine to blue
ax.spines['bottom'].set_linewidth(2)  # Set the thickness of the bottom spine to 2
ax.spines['bottom'].set_visible(True)  # Make the bottom spine visible (if previously hidden)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Customized Bottom Spine')
plt.show()
輸出
Bottom Spine

底部脊柱自定義的用例

強調軸 - 透過自定義底部脊柱可以吸引人們注意 x 軸並增強繪圖的美觀性。

突出顯示繪圖邊界 - 透過調整底部脊柱的外觀可以幫助描繪繪圖區域並提高其清晰度。

左側脊柱

在 Matplotlib 中,左側脊柱指的是構成繪圖區域左側邊框的垂直線,對應於 y 軸。

左側脊柱的特徵

與 y 軸關聯 - 左側脊柱表示沿 y 軸的繪圖邊界,定義了繪圖區域的左邊界。

自定義 - 左側脊柱的自定義類似於其他脊柱,可以透過顏色、可見性、邊框寬度等進行自定義。

自定義左側脊柱的示例

在此示例中,ax.spines['left'].set_color('green') 將左側脊柱的顏色更改為綠色,ax.spines['left'].set_linewidth(2) 將左側脊柱的粗細設定為 2,並且ax.spines['left'].set_visible(False) 確保左側脊柱不可見(如果它可見)。

import matplotlib.pyplot as plt

# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)

# Accessing and customizing the left spine
ax = plt.gca()  # Get the current axes
ax.spines['left'].set_color('green')  # Change the color of the left spine to green
ax.spines['left'].set_linewidth(2)  # Set the thickness of the left spine to 2
ax.spines['left'].set_visible(False)  # Make the left spine invisible (if previously visible)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Customized Left Spine')
plt.show()
輸出
Left Spine

右側脊柱

在 Matplotlib 中,右側脊柱表示構成繪圖區域右側邊框的垂直線,對應於右側的 y 軸。

右側脊柱的特徵

與 y 軸關聯 - 右側脊柱定義了沿 y 軸的繪圖右側邊界,表示繪圖右側的 y 軸。

自定義 - 與其他脊柱類似,右側脊柱可以在其可見性、顏色、線型、粗細和位置方面進行自定義。

自定義右側脊柱的示例

在此示例中,我們使用ax.spines['right'] 來自定義繪圖的右側脊柱。

import matplotlib.pyplot as plt

# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)

# Accessing and customizing the right spine
ax = plt.gca()  # Get the current axes
ax.spines['right'].set_color('green')  # Change the color of the right spine to green
ax.spines['right'].set_linewidth(2)  # Set the thickness of the right spine to 2
ax.spines['right'].set_visible(True)  # Make the right spine visible (if previously hidden)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Customized Right Spine')
plt.show()
輸出
Right Spine

自定義 Matplotlib 圖形的脊柱

在此示例中,我們建立了六個圖形以檢視並自定義它們的脊柱。

示例

#First import the required libraries for the workbook.
import numpy as np
import matplotlib.pyplot as plt

#draw graph for sines
theta = np.linspace(0, 2*np.pi, 128)
y = np.sin(theta)
fig = plt.figure(figsize=(8,6))

#Define the axes with default spines
ax1 = fig.add_subplot(2, 3, 1)
ax1.plot(theta, np.sin(theta), 'b-*')
ax1.set_title('default spines')

#Define the function to plot the graph
def plot_graph(axs, title, lposition, bposition):
   ax = fig.add_subplot(axs)
   ax.plot(theta, y, 'b-*')
   ax.set_title(title)
   ax.spines['left'].set_position(lposition)
   ax.spines['right'].set_visible(False)
   ax.spines['bottom'].set_position(bposition)
   ax.spines['top'].set_visible(False)
   ax.xaxis.set_ticks_position('bottom')
   ax.yaxis.set_ticks_position('left')

#plot 3 graphs
plot_graph(232, 'centered spines', 'center', 'center')
plot_graph(233, 'zeroed spines', 'zero', 'zero')
plot_graph(234, 'spines at axes [0.25, 0.75]', ('axes', 0.25),('axes', 0.75))
plot_graph(235, 'spines at data [1.0, -1.0]', ('data', 1.0),('data', -1.0))
plot_graph(236, 'adjusted spines', ('outward', 10), ('outward', 10))

#fit the plot in the grid and display.
plt.tight_layout()
plt.show()
輸出
Customize Spines
廣告