seaborn.JointGrid#

class seaborn.JointGrid(data=None, *, x=None, y=None, hue=None, height=6, ratio=5, space=0.2, palette=None, hue_order=None, hue_norm=None, dropna=False, xlim=None, ylim=None, marginal_ticks=False)#

用於繪製雙變數圖和邊際單變數圖的網格。

可以使用圖形層級介面 jointplot() 繪製許多圖形。當您需要更高的彈性時,請直接使用此類別。

__init__(data=None, *, x=None, y=None, hue=None, height=6, ratio=5, space=0.2, palette=None, hue_order=None, hue_norm=None, dropna=False, xlim=None, ylim=None, marginal_ticks=False)#

設定子圖網格並在內部儲存資料,以便輕鬆繪圖。

參數:
datapandas.DataFramenumpy.ndarray、映射或序列

輸入資料結構。可以是可指派給具名變數的長格式向量集合,也可以是將在內部重塑的寬格式資料集。

x, ydata 中的向量或鍵

指定 x 和 y 軸位置的變數。

height數字

圖形每一邊的大小(以英寸為單位,將會是正方形)。

ratio數字

聯合軸高度與邊際軸高度的比率。

space數字

聯合軸和邊際軸之間的間隔

dropna布林值

如果為 True,則在繪圖前移除遺失的觀測值。

{x, y}lim數字對

在繪圖前將軸限制設定為這些值。

marginal_ticks布林值

如果為 False,則抑制邊際圖計數/密度軸上的刻度。

huedata 中的向量或鍵

語意變數,已對應以決定繪圖元素的顏色。注意:與 FacetGridPairGrid 不同的是,軸層級函式必須支援 hue,才能在 JointGrid 中使用它。

palette字串、清單、字典或 matplotlib.colors.Colormap

用於選擇在對應 hue 語意時所使用的顏色的方法。字串值會傳遞至 color_palette()。清單或字典值表示類別對應,而色彩對應物件表示數值對應。

hue_order字串向量

指定 hue 語意之類別層級的處理和繪圖順序。

hue_norm元組或 matplotlib.colors.Normalize

一組設定資料單位正規化範圍的值,或一個將資料單位對應至 [0, 1] 間隔的物件。使用表示數值對應。

另請參閱

jointplot

繪製具有單變數邊際分佈的雙變數圖。

PairGrid

設定一個圖形,其中包含多個變數的聯合和邊際檢視。

jointplot

繪製具有單變數邊際分佈的多個雙變數圖。

範例

呼叫建構函式會初始化圖形,但不會繪製任何內容

penguins = sns.load_dataset("penguins")
sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
../_images/JointGrid_1_0.png

最簡單的繪圖方法,JointGrid.plot() 接受一對函數(一個用於聯合軸,另一個用於邊緣軸)

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot(sns.scatterplot, sns.histplot)
../_images/JointGrid_3_0.png

JointGrid.plot() 函數也接受額外的關鍵字參數,但它會將這些參數傳遞給兩個函數

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot(sns.scatterplot, sns.histplot, alpha=.7, edgecolor=".2", linewidth=.5)
../_images/JointGrid_5_0.png

如果您需要將不同的關鍵字參數傳遞給每個函數,則必須調用 JointGrid.plot_joint()JointGrid.plot_marginals()

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot_joint(sns.scatterplot, s=100, alpha=.5)
g.plot_marginals(sns.histplot, kde=True)
../_images/JointGrid_7_0.png

您也可以在不分配任何資料的情況下設定網格

g = sns.JointGrid()
../_images/JointGrid_9_0.png

然後,您可以透過存取 ax_jointax_marg_xax_marg_y 屬性來繪圖,這些屬性是 matplotlib.axes.Axes 物件

g = sns.JointGrid()
x, y = penguins["bill_length_mm"], penguins["bill_depth_mm"]
sns.scatterplot(x=x, y=y, ec="b", fc="none", s=100, linewidth=1.5, ax=g.ax_joint)
sns.histplot(x=x, fill=False, linewidth=2, ax=g.ax_marg_x)
sns.kdeplot(y=y, linewidth=2, ax=g.ax_marg_y)
../_images/JointGrid_11_0.png

繪圖方法可以使用任何接受 xy 變數的 seaborn 函數

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot(sns.regplot, sns.boxplot)
../_images/JointGrid_13_0.png

如果函數接受 hue 變數,您可以在呼叫建構函式時指定 hue 來使用它

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
g.plot(sns.scatterplot, sns.histplot)
../_images/JointGrid_15_0.png

可以使用 :meth:JointGrid.refline 將水平和/或垂直參考線新增到聯合軸和/或邊緣軸

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot(sns.scatterplot, sns.histplot)
g.refline(x=45, y=16)
../_images/JointGrid_17_0.png

圖形始終為正方形(除非您在 matplotlib 層調整其大小),但其整體大小和佈局是可配置的。大小由 height 參數控制。聯合軸和邊緣軸之間的相對比例由 ratio 控制,而圖之間的空間量由 space 控制

sns.JointGrid(height=4, ratio=2, space=.05)
../_images/JointGrid_19_0.png

預設情況下,邊緣圖的密度軸上的刻度會關閉,但這是可配置的

sns.JointGrid(marginal_ticks=True)
../_images/JointGrid_21_0.png

設定圖形時,也可以定義兩個資料軸的限制(在圖中共享)

sns.JointGrid(xlim=(-2, 5), ylim=(0, 10))
../_images/JointGrid_23_0.png

方法

__init__([data, x, y, hue, height, ratio, ...])

設定子圖網格並在內部儲存資料,以便輕鬆繪圖。

apply(func, *args, **kwargs)

將網格傳遞給使用者提供的函數並返回 self。

pipe(func, *args, **kwargs)

將網格傳遞給使用者提供的函數並傳回其值。

plot(joint_func, marginal_func, **kwargs)

透過傳遞聯合軸和邊緣軸的函數來繪製圖形。

plot_joint(func, **kwargs)

在網格的聯合軸上繪製雙變數圖。

plot_marginals(func, **kwargs)

在每個邊緣軸上繪製單變數圖。

refline(*[, x, y, joint, marginal, color, ...])

將參考線新增至聯合軸和/或邊緣軸。

savefig(*args, **kwargs)

儲存圖形的影像。

set(**kwargs)

在每個子圖軸上設定屬性。

set_axis_labels([xlabel, ylabel])

在雙變數軸上設定軸標籤。

屬性

fig

已棄用:建議使用 figure 屬性。

figure

存取網格底層的 matplotlib.figure.Figure 物件。