Is there an alternate for 'palette' while plotting barplot through seaborn object Plot(); How to remove legend when plotting with so.Plot()

172 views Asked by At

I am trying to learn the seaborn object Plot() interface. With the traditional sns.barplot() we could easily define color palette for the bars.

import seaborn as sns
df = sns.load_dataset('penguins')
sns.barplot(df, x='species', y='body_mass_g', palette='YlGnBu')

enter image description here However, When I am trying to achieve the same with the use of Plot(),

so.Plot(df, x='species', y='body_mass_g').add(so.Bar(palette='YlGnBu'),so.Agg())

I am getting the following error:

TypeError: Bar.__init__() got an unexpected keyword argument 'palette'

I could able to assign colors to the bars by declaring color='species' and passing the color list in scale().

so.Plot(df, x='species', y='body_mass_g').add(so.Bar(),so.Agg(),color='species').scale(color=['Yellow','Green','Blue'])

enter image description here

However, this comes with a legend. I tried to remove the legend by plt.legend_.remove(), but it's not working.

1

There are 1 answers

0
mwaskom On BEST ANSWER

You can suppress each layer from the legend when you call Plot.add. As an aside, you can also set a named palette for the color scale:

import seaborn as sns
df = sns.load_dataset('penguins')
(
    so.Plot(df, x='species', y='body_mass_g')
    .add(so.Bar(),so.Agg(), color='species', legend=False)
    .scale(color="YlGnBu")
)

enter image description here