Matplotlib - 座標軸範圍



什麼是座標軸範圍?

在Matplotlib中,座標軸範圍指的是繪圖中x軸和y軸上顯示的值的範圍。這些範圍決定了繪圖區域內可見的資料,並由每個軸上顯示的最小值和最大值定義。

自定義Matplotlib中的座標軸範圍可以更好地控制繪圖中可見的資料,透過突出顯示特定範圍或模式來增強視覺化效果。

關於座標軸範圍的關鍵點

X軸和Y軸範圍 - 可以獨立調整x軸和y軸的座標軸範圍以顯示資料的特定部分。

設定範圍 - 我們可以使用諸如plt.xlim()plt.ylim()之類的函式,或者在使用軸物件時使用等效方法ax.set_xlim()ax.set_ylim()來手動設定座標軸範圍。

自動範圍 - 預設情況下,Matplotlib庫會根據提供的資料自動計算並設定座標軸範圍。但是,手動調整允許關注特定資料範圍或增強視覺化效果。

座標軸範圍的用例

放大或縮小 - 調整座標軸範圍以關注資料的特定部分,從而在繪圖中放大或縮小。

資料重點 - 突出顯示資料的特定範圍以強調模式或趨勢。

避免雜亂 - 我們可以調整繪圖座標軸的範圍,以防止資料點重疊,從而改善繪圖某些部分的視覺化效果。

座標軸範圍的關鍵概念

以下是繪圖座標軸範圍的關鍵概念。讓我們詳細瞭解每個概念。

X軸範圍

座標軸範圍中的x範圍指的是繪圖中沿x軸顯示的值的範圍。它透過定義該維度中顯示的資料範圍來確定水平軸上可見的最小值和最大值。

在Matplotlib庫中,我們可以使用plt.xlim()ax.set_xlim()來設定x軸範圍,以指定x軸的限制。這使我們可以控制沿x軸顯示的資料範圍。

控制座標軸範圍中的x範圍為沿x軸視覺化資料的特定部分提供了靈活性,從而可以更好地解釋和分析所繪製的資訊。

使用plt.xlim()的示例

在這個例子中,plt.xlim(1, 5)將x軸的限制設定為1到5,透過定義繪圖中可見的x範圍來覆蓋沿x軸1到5之間的資料點。

import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a plot
plt.plot(x, y)
# Setting x-axis limits (x-range)
plt.xlim(1, 5)  # Set x-axis limits from 1 to 5
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Custom X-axis Range')
plt.show()
輸出
pltxlim

使用ax.set_xlim()的示例

這是一個演示使用ax.set_xlim()在使用軸物件ax時設定x軸限制的示例。

import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a figure and axis
fig, ax = plt.subplots()
# Plotting on the axis
ax.plot(x, y)
# Setting x-axis limits using axis object
ax.set_xlim(0, 6)  # Set x-axis limits from 0 to 6
# Labeling axes and adding title
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Plot with Custom X-axis Limits')
plt.show()
輸出
ax_xlim

Y軸範圍

在Matplotlib中設定y軸範圍或限制允許我們指定繪圖垂直y軸上顯示的值的範圍。我們可以控制y軸上顯示的最小值和最大值,以關注特定資料範圍或模式。在Matplotlib中設定y軸的範圍或限制可以使用plt.ylim()函式或在使用軸物件ax時使用ax.set_ylim()方法。沿垂直y軸顯示的值的範圍。

設定y軸範圍有助於關注特定資料範圍、突出顯示趨勢並確保視覺化效果強調y軸上資料的相關部分。

使用plt.ylim()設定Y軸範圍的示例

在這個例子中,plt.ylim(0, 12)函式將y軸的限制設定為0到12,指定了y軸上顯示的值的範圍。

import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a plot
plt.plot(x, y)
# Setting y-axis limits
plt.ylim(0, 12)  # Set y-axis limits from 0 to 12
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Custom Y-axis Range')
plt.show()
輸出
ax_ylim

在子圖中使用ax.set_ylim()設定Y軸範圍的示例

在使用軸物件ax時,我們可以根據需要為特定子圖設定y軸限制。

在這個例子中,ax.set_ylim(0, 20)將y軸的限制設定為0到20,專門用於子圖或軸ax。

import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating subplots
fig, ax = plt.subplots()
# Plotting on the axis
ax.plot(x, y)
# Setting y-axis limits using axis object
ax.set_ylim(0, 20)  # Set y-axis limits from 0 to 12
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Plot with Custom Y-axis Range')
plt.show()
輸出
ax_ylim

更改X軸和Y軸的範圍

此示例更改X軸和Y軸的範圍,我們可以使用xlim()和ylim()方法。

示例

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-15, 15, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlim(-10, 10)
plt.ylim(-1, 1)
plt.show()
輸出
x_y_limits
廣告