Incorrect Legend Labels in Marine Python Areas

enter image description here

The above chart is made using a sea python python. However, I’m not sure why some of the legendary circles are filled with color, while others are not. This is the color I use:

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)

- EDIT:

Is there a way that circles can be filled? The reason they are not filled is because they may not have data in this particular plot.

+4
source share
1 answer

Circles are not filled when there is no data, as I think you have already withdrawn. But it can be made to manipulate the object of the legend.

Full example:

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()

The important part is handling the objects handleat legendthe end (in the for loop).

:

enter image description here

( for):

enter image description here

: , !

+6

Source: https://habr.com/ru/post/1682510/


All Articles