在下面基于 seaborn 的绘图中,我制作了一个由群图覆盖的箱形图。两者都是色调子集。有什么办法可以不让它们在图例中重复两次吗?
这是我的代码:
ax = sns.boxplot(x=name_xaxis, y=name_col, hue=hue, data=frame, palette='Set2', linewidth=1.5, width=0.5)
sns.swarmplot(x=name_xaxis, y=name_col, hue=hue, data=frame, palette='Set2', color='.25', split=True)
最佳答案
尝试在 sns.swarmplot(...)
之后添加:
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[:2], labels[:2])
这应该用现有图例中的两个条目替换图例。
关于python - 避免在 swarmplot 覆盖的 seaborn boxplot 中重复图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36267705/