python seaborn 图中的图例标签不正确

Incorrect legend labels in python seaborn plots

上图是在python中使用seaborn制作的。但是,不确定为什么有些图例圆圈是用颜色填充的,而另一些则不是。这是我正在使用的颜色图:

sns.color_palette("Set2", 10)

g = sns.factorplot(x='month', y='vae_factor', hue='ad_name', col='crop', data=df_sub_panel,
                   col_wrap=3, size=5, lw=0.5, ci=None, capsize=.2, palette=sns.color_palette("Set2", 10),
                   sharex=False, aspect=.9, legend_out=False)
g.axes[0].legend(fancybox=None)

--编辑:

有什么方法可以填满圆圈吗?他们没有被填满的原因是他们在这个特定的情节中可能没有数据

当没有数据时,圆圈不会被填充,我想你已经推断出来了。但是可以通过操作图例对象来强制。

完整示例:

import pandas as pd
import seaborn as sns

df_sub_panel = pd.DataFrame([
  {'month':'jan', 'vae_factor':50, 'ad_name':'China', 'crop':False},
  {'month':'feb', 'vae_factor':60, 'ad_name':'China', 'crop':False},
  {'month':'feb', 'vae_factor':None, 'ad_name':'Mexico', 'crop':False},
])

sns.color_palette("Set2", 10)

g = sns.factorplot(x='month', y='vae_factor', hue='ad_name', col='crop', data=df_sub_panel,
                   col_wrap=3, size=5, lw=0.5, ci=None, capsize=.2, palette=sns.color_palette("Set2", 10),
                   sharex=False, aspect=.9, legend_out=False)

# fill in empty legend handles (handles are empty when vae_factor is NaN)
for handle in g.axes[0].get_legend_handles_labels()[0]:
  if not handle.get_facecolors().any():
    handle.set_facecolor(handle.get_edgecolors())

legend = g.axes[0].legend(fancybox=None)

sns.plt.show()

重要的部分是最后(在 for 循环中)legend 中的 handle 对象的操作。

这将生成:

与原来的相比(没有for循环):

编辑: 由于评论中的建议,现在不那么老套了!