小编Wol*_*d72的帖子

自定义 Seaborn histplot 子图中的图例

我正在尝试生成一个包含 4 个子图的图形,每个子图都是 Seaborn 直方图。图形定义线为:

fig,axes=plt.subplots(2,2,figsize=(6.3,7),sharex=True,sharey=True)
(ax1,ax2),(ax3,ax4)=axes
fig.subplots_adjust(wspace=0.1,hspace=0.2)
Run Code Online (Sandbox Code Playgroud)

我想为每个子图中的图例条目定义字符串。作为示例,我对第一个子图使用以下代码:

sp1=sns.histplot(df_dn,x="ktau",hue="statind",element="step", stat="density",common_norm=True,fill=False,palette=colvec,ax=ax1)
ax1.set_title(r'$d_n$')
ax1.set_xlabel(r'max($F_{a,max}$)')
ax1.set_ylabel(r'$\tau_{ken}$')
legend_labels,_=ax1.get_legend_handles_labels()
ax1.legend(legend_labels,['dep-','ind-','ind+','dep+'],title='Stat.ind.')
Run Code Online (Sandbox Code Playgroud)

图例未正确显示(图例条目未绘制,图例标题是色调变量的名称(“statind”)。请注意,我已成功对其他图形使用相同的代码,其中我使用 Seaborn relplots 而不是 histplots 。

python matplotlib legend seaborn

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

legend ×1

matplotlib ×1

python ×1

seaborn ×1