[英]How to add comparison lines to an lmplot in seaborn?
我想結合我擁有的以下 lmplots。 更具體地說,紅線是每個季節的平均值,我想將它們與其他數據放在各自的 lmplots 上,而不是將它們分開。 這是我的代碼(注意,軸限制不起作用,因為第二個 lmplot 搞砸了。當我只是 plot 初始數據時它起作用):
ax = sns.lmplot(data=data, x='air_yards', y='cpoe',col='season', lowess = True, scatter_kws={'alpha':.6, 'color': '#4F2E84'}, line_kws={'alpha':.6, 'color': '#4F2E84'})
ax = sns.lmplot(data=avg, x='air_yards', y= 'cpoe',lowess=True, scatter=False, line_kws={'linestyle':'--', 'color': 'red'}, col = 'season')
axes.set_xlim([-5,30])
axes.set_ylim([-25,25])
ax.set(xlabel='air yards')
不確定它是否可能是您想要的方式,所以可能是這樣的:
import matplotlib.pyplot as plt
import seaborn as sns
#dummy example
data = pd.DataFrame({'air_yards': range(1,11),
'cpoe': range(1,11),
'season': [1,2,3,2,1,3,2,1,3,2]})
avg = pd.DataFrame({'air_yards': [1, 10]*3,
'cpoe': [2,2,5,5,8,8],
'season': [1,1,2,2,3,3]})
# need this info
n = data["season"].nunique()
# create the number of subplots
fig, axes = plt.subplots(ncols=n, sharex=True, sharey=True)
# now you need to loop through unique season
for ax, (season, dfg) in zip(axes.flat, data.groupby("season")):
# set title
ax.set_title(f'season={season}')
# create the replot for data
sns.regplot("air_yards", "cpoe", data=dfg, ax=ax,
lowess = True, scatter_kws={'alpha':.6, 'color': '#4F2E84'},
line_kws={'alpha':.6, 'color': '#4F2E84'})
# create regplot for avg
sns.regplot("air_yards", "cpoe", data=avg[avg['season'].eq(season)], ax=ax,
lowess=True, scatter=False,
line_kws={'linestyle':'--', 'color': 'red'})
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.