繁体   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