Skip to main content
 首页 » 编程设计

python之增加 Seaborn 联合图中的文本大小

2025年05月04日14hnrainll

我正在使用 Seaborn 的 jointplot 生成一些散点图。

这是我所做的示例及其结果:

from scipy import stats 
 
x = np.arange(100) + np.random.randn(100)*20 
y = np.arange(100) + np.random.randn(100)*20 
 
g = sb.JointGrid(x, y, ratio=100) 
g.plot_joint(sb.regplot) 
g.annotate(stats.spearmanr) 
g.ax_marg_x.set_axis_off() 
g.ax_marg_y.set_axis_off() 
plt.xlabel('X', fontsize=18) 
plt.ylabel('Y', fontsize=18) 
plt.tick_params(axis="both", labelsize=18) 
plt.legend(fontsize=20) 

这是结果图。

如何更改图中显示的文本大小(相关系数和 p 值的文本)?

请您参考如下方法:

Seaborn 页面中的这个示例很好地解释了这一点。

g = sns.JointGrid(x="total_bill", y="tip", data=tips) 
g = g.plot_joint(plt.scatter, 
             color="g", s=40, edgecolor="white") 
g = g.plot_marginals(sns.distplot, kde=False, color="g") 
rsquare = lambda a, b: stats.pearsonr(a, b)[0] ** 2 
g = g.annotate(rsquare, template="{stat}: {val:.2f}", 
           stat="$R^2$", loc="upper left", fontsize=12) 

所以你可以试试这个

g = sns.jointplot(x="", y="", data=df,kind='reg'); 
g = g.annotate(stats.pearsonr, fontsize=18)