簡體   English   中英

在 Seaborn Jointplot 上繪制對角線(相等線)

[英]Drawing Diagonal line (line of equality) on Seaborn Jointplot

我正在使用 seaborn 關節圖進行散點圖繪制,但我似乎無法得到一條簡單的對角線……我得到了一個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"

提前謝謝大家。

錯誤是一個有用的提示:一個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()

聯合圖顯示了“山”的密度視圖,沿 X 軸和 Y 軸的投影繪制在上方和右側。 x=y 線以紅色繪制。

我在解釋器中發現了這一點: dir(g) ,然后是g.plot? , g.plot_joint? -- 這些是特定於聯合圖的繪圖函數 -- 還有什么? -- dir(g.ax_joint) ; 啊哈,有set_ylim等。

對角線是 x=y 線,但請注意,它不是中心圖像素的 45deg 對角線。 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()

上一個圖,另外繪制了兩條線,一條是中心圖的 45 度對角線,另一條是 x=y。此外 (0,0) 和 (2,2) 已被繪制為點以驗證 x=y。

暫無
暫無

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

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