Python 在Seaborn接合处绘制对角线(等分线)

Python 在Seaborn接合处绘制对角线(等分线),python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我使用seaborn连接图进行散点绘制,但我似乎无法得到一条简单的对角线穿过。。。我得到一个属性错误:“JointGrid”对象没有属性“get\u xlim”。有人知道使用Seaborn的变通方法吗 这是我的代码(标题也没有显示!给出了什么): 提前感谢大家。错误是一个有用的提示:JointGrid组织了几个子地块,您必须找到要打印的特定的ax: import numpy as np import pandas as pd import seaborn as sns from matplotl

我使用seaborn连接图进行散点绘制,但我似乎无法得到一条简单的对角线穿过。。。我得到一个
属性错误:“JointGrid”对象没有属性“get\u xlim”
。有人知道使用Seaborn的变通方法吗

这是我的代码(标题也没有显示!给出了什么):


提前感谢大家。

错误是一个有用的提示:JointGrid组织了几个子地块,您必须找到要打印的特定的
ax

import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.pyplot import show
sns.set_style('darkgrid')

# Generate a random correlated bivariate dataset
# https://stackoverflow.com/questions/16024677/generate-correlated-data-in-python-3-3

rs = np.random.RandomState(5)
mean = [0, 0]
cov = [[2, .1],
       [.5, 3]]  # make it asymmetric as a better test of x=y line
y = np.random.multivariate_normal(mean, cov, 500, tol=1e-4)

# Show the joint distribution using kernel density estimation
g = sns.jointplot(x=y[:,0], y=y[:,1],
                  kind="kde",
                  fill="true", height=5, space=0, levels=7)

# Draw a line of x=y 
x0, x1 = g.ax_joint.get_xlim()
y0, y1 = g.ax_joint.get_ylim()
lims = [max(x0, y0), min(x1, y1)]
g.ax_joint.plot(lims, lims, '-r')
show()

我在一个解释器中弄明白了这一点:
dir(g)
,然后是
g.plot?
g.plot\u joint?
——这些是特定于jointplot的绘图函数——还有什么<代码>方向(g.ax_接头);啊哈,这里有
set\u ylim
等等

对角线是x=y线,但请注意,它不是中心图像素的45度对角线。seaborn
jointplot
函数始终绘制一个方形中心图。当数据不对称时,绘图的X轴和Y轴将改变以适合正方形。变量
lims
以数据坐标保存显示器的边缘

有一条注释建议绘制一条对角线,该对角线始终是显示的对角线,但它不是数据的等分线。下面是要添加的几行代码来测试这一点,并找出您想要的代码:

# This will always go from corner to corner of the displayed plot
g.ax_joint.plot([0,1], [0,1], ':y', transform=g.ax_joint.transAxes)

# This is the x=y line using transforms
g.ax_joint.plot(lims, lims, 'w', linestyle='dashdot', transform=g.ax_joint.transData)

# This is a simple way of checking which is x=y
g.ax_joint.scatter(x=[0, 2],y=[0,2], s=30,c= 'w',marker='o')
show()

我几乎是一字不差地完成这项工作,但这一行不会显示出来。我还需要做什么才能在2019年完成这项工作?您的jointplot很好,但附加行不会显示?我只是在ipython中重新运行了几次,它成功了(只更新了一个参数名)。我们可能需要比较库/语言版本号。或者您是否收到任何错误消息?这是因为用于绘制对角线的最后一个代码块是错误的。从中,您可能更愿意使用:
g.ax\u joint.plot([0,1],[0,1],':k',transform=g.ax\u joint.transAxes)
# This will always go from corner to corner of the displayed plot
g.ax_joint.plot([0,1], [0,1], ':y', transform=g.ax_joint.transAxes)

# This is the x=y line using transforms
g.ax_joint.plot(lims, lims, 'w', linestyle='dashdot', transform=g.ax_joint.transData)

# This is a simple way of checking which is x=y
g.ax_joint.scatter(x=[0, 2],y=[0,2], s=30,c= 'w',marker='o')
show()