0

I have a boxplot and added a dot for each mean using the meanprops argument. I'd like to add a legend that tells people that the little red dot is the mean, but when I add the legend, it shows a duplicate legend entry for every box plot! Is there any way to have a legend with only a single item in it instead of a bajillion?

plt.figure(figsize=(20, 10))
sns.boxplot(data=country_ratings_df,
            x='Country',
            y='Stars',
            order=country_ratings_grouped.index,
            showmeans=True,
            meanprops={'marker':'o',
                       'markerfacecolor':'red', 
                       'markeredgecolor':'black',
                       'markersize':'7',
                       'alpha':0.5,
                       'label':'Mean'})
plt.title('Ramen Ratings by Country (Sorted by Mean Rating)', size=20)
plt.xlabel('Country', size=20)
plt.ylabel('Stars', size=20)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.grid(axis='x')
plt.xticks(rotation=90)
plt.legend()
plt.show()

Many ramen means!

  • You can do this by getting the legend handle and label, and then doing the first one. `ax = sns.boxplot(...);handles, labels = ax.get_legend_handles_labels();plt.legend(handles[0:1], labels[0:1], bbox_to_anchor=(1.02, 1), loc=2, borderaxespad=0.)` – r-beginners Jan 15 '22 at 04:49

0 Answers0