Remove Legendary Barbot

I use seaborn to plot grouped strokes, as in https://seaborn.pydata.org/examples/factorplot_bars.html

Giving me: https://seaborn.pydata.org/_images/factorplot_bars.png

there is a name (gender) in the legend that I would like to remove.

How could I achieve this?

+5
source share
3 answers

It may be a hacked solution, but it works: if you say that Siborn will leave it while plotting and then add it back, it does not have the name of the legend:

g = sns.factorplot(x='Age Group',y='ED',hue='Became Member',col='Coverage Type', col_wrap=3,data=gdf,kind='bar',ci=None,legend=False,palette='muted') # ^^^^^^^^^^^^ plt.suptitle('ED Visit Rate per 1,000 Members per Year',size=16) plt.legend(loc='best') plt.subplots_adjust(top=.925) plt.show() 

Result:

enter image description here

+4
source

A less dangerous way is to use the object-oriented matplotlib interface. Having gained control over the axes, this will greatly simplify the schedule setting.

 import seaborn as sns import matplotlib.pyplot as plt sns.set(style="whitegrid") # Load the example Titanic dataset titanic = sns.load_dataset("titanic") # Draw a nested barplot to show survival for class and sex fig, ax = plt.subplots() g = sns.factorplot(x="class", y="survived", hue="sex", data=titanic, size=6, kind="bar", palette="muted", ax=ax) sns.despine(ax=ax, left=True) ax.set_ylabel("survival probability") l = ax.legend() l.set_title('Whatever you want') fig.show() 

Results in result_plot

+4
source

If you want the legend to appear outside the axis of the graph, by default for factorplot , you can use FacetGrid.add_legend ( factorplot returns an instance of FacetGrid ). Other methods allow you to immediately adjust the labels of each axis in FacetGrid

 import seaborn as sns import matplotlib.pyplot as plt sns.set(style="whitegrid") # Load the example Titanic dataset titanic = sns.load_dataset("titanic") # Draw a nested barplot to show survival for class and sex g = sns.factorplot(x="class", y="survived", hue="sex", data=titanic, size=6, kind="bar", palette="muted", legend=False) (g.despine(left=True) .set_ylabels('survival probability') .add_legend(title='Whatever you want') ) 
0
source

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


All Articles