如何在lmplot
中注释文本?我想展示“花瓣长度”与虹膜数据集中其他特征之间的相关性,所以我用lmplot
绘制了再生长图
import seaborn as sns
import pandas as pd
df = sns.load_dataset('iris')
melt = pd.melt(df, id_vars=['species','petal_length'], value_vars=['sepal_length','sepal_width', 'petal_width'])
sns.lmplot(data=melt, x='value', y='petal_length', col='variable', sharey=False, sharex=False)
但是,我不知道如何注释相关值。我可以用一个regplot
来完成,如下所示:
from scipy.stats import spearmanr
r, pvalue = spearmanr(df['sepal_length'], df['petal_length'])
sns.regplot(data=df, x='sepal_length', y='petal_length', label=f'Spearman = {r:.2f}')
plt.legend()
lmplot
返回一个FaceGrid,因此我必须在每个轴上注释文本如何在FacetGrid
上注释值列表?
spearman = []
for feature in ['sepal_length','sepal_width', 'petal_width']:
r, pvalue = spearmanr(df['petal_length'], df[feature])
spearman.append(r)
print(spearman)
[0.8818981264349859,-0.30963508601557777,0.9376668235763412]
可以在轴之间循环,计算r值并将其添加到图例中:
PS:除了创建图例,您还可以更新标题,例如
相关问题 更多 >
编程相关推荐