在Seaborn Jointplot上绘制对角线(平等线)

时间:2015-07-13 19:13:05

标签: python matplotlib seaborn

我使用seaborn jointplot进行散点图绘制,但我似乎无法得到一条简单的对角线......我得到的是AttributeError: 'JointGrid' object has no attribute 'get_xlim'。有没有人知道使用Seaborn的解决方法?

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

ax = sns.jointplot(x="Av Tomato-meter", y="Av Audience Score", data=director_combined_ratings, stat_func = None, 
                   size = 8, xlim=(0,100), ylim=(0,100))

ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3") #this is the error line.

ax.title = "Average Tomato-meter vs Audience Score for Directors with over 10 Movies"

先谢谢大家。

1 个答案:

答案 0 :(得分:16)

错误是一个有用的暗示:一个JointPlot是子图的集合,你必须找到特定的斧头来绘制。修改Seaborn示例:

import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.pyplot import show
sns.set(style="white")

# Generate a random correlated bivariate dataset
rs = np.random.RandomState(5)
mean = [0, 0]
cov = [(2, .25), (.5, 1)] # make it asymmetric as a better test of x=y line
x1, x2 = rs.multivariate_normal(mean, cov, 500).T
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")

# Show the joint distribution using kernel density estimation
g = sns.jointplot(x1, x2, kind="kde", size=7, space=0)

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, ':k')    
show()

enter image description here

我在翻译中找到了这个:dir(g),然后是g.plot?g.plot_joint? - 那些正在绘制特定于联合图的功能 - 还有什么? - dir(g.ax_joint);啊哈,有set_ylim等等。