Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seaborn violinplots: change violin color, axes names, legend

I used to create all my plots using matplotlib until I recently discovered seaborn. I found particularly interesting the fact that it allows you to split violins in order to compare a given hue variable.

Well, my major problem is that I don't know what do I have to modify in order to change violin colors, axes names, or place the legend where I want.

Here is the example I followed from seaborn:

    import seaborn as sns
    tips = sns.load_dataset("tips")
    sns.set(style="ticks", palette="colorblind")
    g = sns.FacetGrid(tips, col="time", size=4, aspect=.75)
    g = g.map(sns.violinplot, "sex", "total_bill", "smoker", inner=None, linewidth=1, scale="area", split=True, width=0.75).despine(left=True).add_legend(title="smoker")
    g.savefig(os.path.join(options.output_dir, "figures", "violinplots.png"))

And here is the output figure violinplots.png:

enter image description here

While I would rather like something like this:

enter image description here

To summarize:

  • use white and blue colors
  • replace axes names
  • write leftmost y axis only
  • make my own legend with the blue category only

Thank you in advance. Any help will be welcome.


In case anyone was interested, here is how I finally resolved the figure thanks to MrPedru22:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(context="paper", palette="colorblind", style="ticks")
g = sns.FacetGrid(tips, col="time", sharey=False, size=4, aspect=.5)
g = g.map(seaborn.violinplot, "sex", "total_bill", "smoker", cut=0, inner=None, split=True, palette={"No": "#4477AA", "Yes": "white"}, saturation=1).despine(left=True)
# Set axis labels & ticks #
g.fig.get_axes()[0].set_xlabel("Lunch")
g.fig.get_axes()[1].set_xlabel("Dinner")
g.fig.get_axes()[0].set_xticklabels(["Male", "Female"])
g.fig.get_axes()[1].set_xticklabels(["Male", "Female"])
g.fig.get_axes()[0].set_ylabel("Total bill")
g.fig.get_axes()[0].set_yticks(range(0, 80, 10))
g.fig.get_axes()[1].set_yticks([])
g.fig.get_axes()[0].spines["left"].set_visible(True)
# Set legend #
handles, labels = g.fig.get_axes()[0].get_legend_handles_labels()
g.fig.get_axes()[0].legend([handles[1]], ["Non-smoker"], loc='upper left')
# Fixing titles #
g.fig.get_axes()[0].set_title("")
g.fig.get_axes()[1].set_title("")
g.plt.show()

enter image description here

like image 425
ofornes Avatar asked Oct 20 '15 19:10

ofornes


1 Answers

Here are some answers:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(style="ticks", palette="colorblind")
g = sns.FacetGrid(tips, col="time", size=4, aspect=.75)
g = g.map(sns.violinplot, "sex", "total_bill", "smoker", palette={"No": "b", "Yes": "w"}, inner=None, linewidth=1, scale="area", split=True, width=0.75).despine(left=True)
g.fig.get_axes()[0].legend(title= 'smoker',loc='upper left')
g.set_axis_labels('lunch','total bill')
sns.plt.show()
  • Use white and blue colors: through pallete, as demonstrated above ("w" means white)
  • Replace axes names: with set_axis_labels method
  • The rest I could not find an answer but please check the documentation: http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.FacetGrid.html#seaborn.FacetGrid Hope it helps!
like image 196
MrPedru22 Avatar answered Oct 17 '22 21:10

MrPedru22