[英]Control order of plotting on Seaborn plot in Python
我正在嘗試在線性回歸圖上繪制殘差。 它起作用,只有一個警告。 殘差和數據點之間存在令人不愉快的重疊。 有沒有辦法告訴matplotlib先繪制殘差,然后再繪制Seaborn圖。 我嘗試更改代碼順序,但沒有幫助。
import numpy as np
import pandas as pd
import seaborn as sns
from pylab import *
from sklearn.linear_model import LinearRegression
x = np.array([1, 2, 3, 4, 5, 7, 8, 9, 10])
y = np.array([-3, 0, 4, 5, 9, 5, 7, 7, 12])
dat = pd.DataFrame({'x': x, 'y': y})
x = x.reshape(-1,1)
y = y.reshape(-1,1)
linear_model = LinearRegression()
linear_model.fit(X=x, y=y)
pred = linear_model.predict(x)
for ix in range(len(x)):
plot([x[ix], x[ix]], [pred[ix], y[ix]], '#C9B97D')
g = sns.regplot(x='x', y='y', data=dat, ci=None, fit_reg=True)
sns.set(font_scale=1.1)
g.figure.set_size_inches(6, 6)
sns.set_style('ticks')
sns.despine()
您要查找的參數是zorder
。 這使您可以控制哪個對象顯示在圖形的頂部。
對於regplot
您必須使用參數scatter_kws
,這是要傳遞給在plt.scatter
使用的scatter_kws
的參數字典。
您的sns.regplot
變為:
g = sns.regplot(x='x', y='y', data=dat, ci=None, fit_reg=True,
scatter_kws={"zorder":10, "alpha":1})
請注意,我已將alpha
設置為1,以使標記不透明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.