簡體   English   中英

將相關系數添加到 seaborn 散點圖 plot

[英]Adding correlation coefficient to a seaborn scatter plot

我目前正在使用sns.scatterplot功能繪制 2 個變量之間的一些數值關系,並希望將 label 添加到散點圖中,該散點圖中顯示 2 個變量之間的相關系數作為我的圖上的注釋。

我將如何在 python/seaborn 中做到這一點?

我嘗試在此處查看 sns 頁面https://seaborn.pydata.org/generated/seaborn.scatterplot.html對於此示例: sns.scatterplot(data=tips, x="total_bill", y="tip")但是找不到任何幫助? 這里有運氣嗎? 謝謝 !

希望這會有所幫助。

# import the scipy library
import scipy as sp
# call the seaborn scatterplot function per usual
sns.scatterplot(data=df, x=df['col1'] y=df['col2'], hue='col3')

# define titles and axes labels
plt.title('Title')
plt.xlabel('x-axis label')
plt.ylabel('y-axis label')

# call the scipy function for pearson correlation
r, p = sp.stats.pearsonr(x=df['col1'] y=df['col2'])
# annotate the pearson correlation coefficient text to 2 decimal places
plt.text(.05, .8, 'r={:.2f}'.format(r), transform=ax.transAxes)

plt.show()

暫無
暫無

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

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