在 Seaborn histplot 子图中自定义图例
Customizing legend in Seaborn histplot subplots
我正在尝试生成一个包含 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)
我想为每个子图中的图例条目定义字符串。例如,我在第一个子图中使用以下代码:
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.')
图例显示不正确(图例条目未绘制,图例标题是 hue 变量的名称(“statind”)。请注意,我已经成功地将相同的代码用于我使用的其他图形Seaborn relplots 而不是 histplots。
主要问题是 ax1.get_legend_handles_labels()
return 的空列表(注意第一个 return 值是句柄,第二个是标签)。至少对于 seaborn 的当前 (0.11.1) 版本 histplot()
.
要获取句柄,您可以执行 legend = ax1.get_legend(); handles = legend.legendHandles
。
要重新创建图例,首先需要删除现有图例。然后,可以从一些句柄开始创建新图例。
另请注意,为了确定标签的顺序,设置hue_order
会有所帮助。下面是一些示例代码来展示这些想法:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
df_dn = pd.DataFrame({'ktau': np.random.randn(4000).cumsum(),
'statind': np.repeat([*'abcd'], 1000)})
fig, ax1 = plt.subplots()
sp1 = sns.histplot(df_dn, x="ktau", hue="statind", hue_order=['a', 'b', 'c', 'd'],
element="step", stat="density", common_norm=True, fill=False, ax=ax1)
ax1.set_title(r'$d_n$')
ax1.set_xlabel(r'max($F_{a,max}$)')
ax1.set_ylabel(r'$\tau_{ken}$')
legend = ax1.get_legend()
handles = legend.legendHandles
legend.remove()
ax1.legend(handles, ['dep-', 'ind-', 'ind+', 'dep+'], title='Stat.ind.')
plt.show()
我正在尝试生成一个包含 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)
我想为每个子图中的图例条目定义字符串。例如,我在第一个子图中使用以下代码:
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.')
图例显示不正确(图例条目未绘制,图例标题是 hue 变量的名称(“statind”)。请注意,我已经成功地将相同的代码用于我使用的其他图形Seaborn relplots 而不是 histplots。
主要问题是 ax1.get_legend_handles_labels()
return 的空列表(注意第一个 return 值是句柄,第二个是标签)。至少对于 seaborn 的当前 (0.11.1) 版本 histplot()
.
要获取句柄,您可以执行 legend = ax1.get_legend(); handles = legend.legendHandles
。
要重新创建图例,首先需要删除现有图例。然后,可以从一些句柄开始创建新图例。
另请注意,为了确定标签的顺序,设置hue_order
会有所帮助。下面是一些示例代码来展示这些想法:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
df_dn = pd.DataFrame({'ktau': np.random.randn(4000).cumsum(),
'statind': np.repeat([*'abcd'], 1000)})
fig, ax1 = plt.subplots()
sp1 = sns.histplot(df_dn, x="ktau", hue="statind", hue_order=['a', 'b', 'c', 'd'],
element="step", stat="density", common_norm=True, fill=False, ax=ax1)
ax1.set_title(r'$d_n$')
ax1.set_xlabel(r'max($F_{a,max}$)')
ax1.set_ylabel(r'$\tau_{ken}$')
legend = ax1.get_legend()
handles = legend.legendHandles
legend.remove()
ax1.legend(handles, ['dep-', 'ind-', 'ind+', 'dep+'], title='Stat.ind.')
plt.show()