簡體   English   中英

如何通過此 seaborn plot 添加 x=y 線?

[英]How do I add an x=y line through this seaborn plot?

我在 seaborn 中有一個對數散點 plot,但我想知道如何用 x=y 線平分數據

ax = sns.pairplot(x_vars=["Yamaguchi Double"], y_vars=["Yamaguchi Helix"], data=df11, 
hue="Image", size=3)
ax.set(xscale="log", yscale="log", xlim=(1e-3,1e1), ylim=(1e-3,1e1))

這會產生這個圖像:

對數 plot

如何在其上添加紅色對角線 x=y 線?

我試過這段代碼:

ax = sns.pairplot(x_vars=["Yamaguchi Double"], y_vars=["Yamaguchi Helix"], data=df11, 
hue="Image", size=3)
ax.set(xscale="log", yscale="log", xlim=(1e-3,1e1), ylim=(1e-3,1e1))
X_plot = np.linspace(0.001, 10)
Y_plot = X_plot
plt.plot(x="X_plot", y="Y_plot", color = 'r')
plt.show()

但這沒有給我

這里要指出的關鍵是sns.pairplot(...)的結果不是軸 object。 相反,它是一個PairGrid object。 我還注意到在設置對數比例之后放置 x=y 行會導致問題,但在設置對數比例之前。

另外,作為旁注,您可能對sns.scatterplotsns.regplot感興趣,它們都返回軸 object 這可能更適合您繪制單個 x,y 散點圖而不是散點圖矩陣的情況,這更多的是sns.pairgrid的用途。

我手頭沒有你的數據,所以這個答案對你來說不是一個完整的復制/粘貼答案,但如果你想用一個適用於類似數字的配對圖來完成這個,下面是你想要做的事情的要點使用企鵝數據集。

import seaborn as sns
import matplotlib.pyplot as plt
penguins = sns.load_dataset("penguins")

# Modify data so x=y line looks interesting in this particular example and works in log space
penguins['bill_length_mm_center'] = penguins["bill_length_mm"] - penguins["bill_length_mm"].mean()
penguins['bill_length_mm_center'] += -penguins["bill_length_mm_center"].min() + 1
penguins['bill_depth_mm_center'] = penguins["bill_depth_mm"] - penguins["bill_depth_mm"].mean()
penguins['bill_depth_mm_center'] += -penguins["bill_depth_mm_center"].min() + 1


g = sns.pairplot(x_vars=['bill_length_mm_center'], y_vars=['bill_depth_mm_center'], hue="species", data=penguins)
def modify_plot(*args, **kwargs):
  """Must take x, y arrays as positional arguments and draw onto the “currently active” 
    matplotlib Axes. Also needs to accept kwargs called color and label.
    We are not using any of these args in this example so just capture them all.
  """
  # The "currently active" matplotlib Axis, unless they decide to pass it to us
  if "ax" in kwargs:
    # Not sure if this is ever used...
    ax = kwargs['ax']
  else:
    ax = plt.gca()
  # Make sure to ax.plot prior to ax.set, for some reason it doesn't work after
  # Oncematplotlib.__version__ >= 3.3 do the following (https://stackoverflow.com/a/73490857/658053)
  # ax.axline((0, 0), slope=1)
  # but the following is similar for earlier versions of matplotlib (https://stackoverflow.com/a/60950862/658053)
  xpoints = ypoints = ax.get_xlim()
  ax.plot(xpoints, ypoints, linestyle='--', color='k', lw=1, scalex=False, scaley=False)

  ax.set(xscale="log", yscale="log")

g.map(modify_plot);

生成的帶有 x=y 線的配對圖

xlims=(1e-3,1e1)
ylims=(1e-3,1e1)

ax = sns.pairplot(x_vars=["Yamaguchi Double"], y_vars=["Yamaguchi Helix"], data=df11, hue="Image", size=3)
ax.set(xscale="log", yscale="log", xlim=xlims, ylim=ylims)
ax.plot(xlims,xlims, color='r')
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM