我很难找到一种方法来正确地仅显示散点图中与标记相关的标签。我的代码如下所示:
fig, ax = plt.subplots(1,1)
plot_white = sns.scatterplot(data=df_white, x='EngCorr_Player', y='EngCorr_Opponent', hue='Elo_Opponent', ax=ax, marker='D', label='White')
plot_black = sns.scatterplot(data=df_black, x='EngCorr_Player', y='EngCorr_Opponent', hue='Elo_Opponent', ax=ax, marker='X', s=140, label='Black')
ax.legend()
plt.show()
如果我改为在调用图例时尝试指定标签,则第二个图的标记是错误的(圆圈,而不是星号)。
ax.legend(labels=['White', 'Black'])
如果我指定手柄,用
ax.legend(handles=[plot_white, plot_black], labels=['White', 'Black'])
显示空图例和错误消息“用户警告:图例不支持
我试图研究艺术家,但没有掌握任何东西。
看看这是否是您正在寻找的……它与您所拥有的相似。除了使用
ax.get_legend_handles_labels()
获取图例的句柄和文本,然后只保留名称为 White 和 Black 的那些,然后调用 ax.legend()
fig, ax = plt.subplots(1,1)
ax = sns.scatterplot(data=df_white, x='EngCorr_Player', y='EngCorr_Opponent', hue='Elo_Opponent', ax=ax, marker='D', label='White')
ax = sns.scatterplot(data=df_black, x='EngCorr_Player', y='EngCorr_Opponent', hue='Elo_Opponent', ax=ax, marker='X', s=140, label='Black')
hand, labl = ax.get_legend_handles_labels()
handout=[]
lablout=[]
for h,l in zip(hand,labl):
if l in ['White', 'Black']:
lablout.append(l)
handout.append(h)
ax.legend(handout, lablout)
plt.show()