Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add split palette and hatching to violinplot? #2027

Closed
lilyminium opened this issue Apr 16, 2020 · 5 comments
Closed

Add split palette and hatching to violinplot? #2027

lilyminium opened this issue Apr 16, 2020 · 5 comments

Comments

@lilyminium
Copy link

lilyminium commented Apr 16, 2020

Hi Seaborn devs,

I modified the violinplot for more flexible palette colouring and hatching when data is split by hue. Would you be interested in adding this functionality to violinplot? If so, I'd be happy to clean it up and make a PR.

Edit: to clarify, there is currently no way to colour by x-axis category when the hue keyword is given, and no current way to add hatching to any of the fills. The modifications are two new keywords, hatch=None and split_palette=False.

Screenshot 2020-04-16 at 10 54 26 PM

Screenshot 2020-04-16 at 11 13 07 PM

@mwaskom
Copy link
Owner

mwaskom commented Apr 16, 2020

I would like to have more hatching in seaborn, however, hatches are kind of broken in matplotlib, both in terms of the API for controlling hatches and their implementation in some of the vector backends. I wouldn't want to add more options for hatching until that improves and stabilizes.

I don't think the split_palette fits with the semantics of violinplot or the way hue otherwise works in seaborn, so I would be -1 on that.

@mwaskom
Copy link
Owner

mwaskom commented Apr 16, 2020

There are many github issues about hatching if you want to read more; here is at least one that I know of because I opened it: matplotlib/matplotlib#11418

@mwaskom
Copy link
Owner

mwaskom commented Apr 16, 2020

By the way here is a method for adding hatches without hacking/extending the violinplot function itself:

import seaborn as sns
import matplotlib as mpl

tips = sns.load_dataset("tips")

ax = sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True)
for i, violin in enumerate(ax.findobj(mpl.collections.PolyCollection)):
    if i % 2:
        violin.set_hatch("//")

ax.legend_.findobj(mpl.patches.Rectangle)[1].set_hatch("///")

image

@lilyminium
Copy link
Author

I don't think the split_palette fits with the semantics of violinplot or the way hue otherwise works in seaborn, so I would be -1 on that.

True, although it would be an optional keyword set to the current behaviour by default.

hatches are kind of broken in matplotlib
I wouldn't want to add more options for hatching until that improves and stabilizes.

Fair enough! That seems to be a no to both changes here, so I'll close the issue.

here is a method for adding hatches hacking/extending the violinplot function itself

Thanks for that, I did look around for the magic way to get the violin objects but focused on methods that started with get. ax.findobj() is very useful to know.

@luyang-ai4med
Copy link

luyang-ai4med commented Aug 7, 2022

By the way here is a method for adding hatches without hacking/extending the violinplot function itself:

import seaborn as sns
import matplotlib as mpl

tips = sns.load_dataset("tips")

ax = sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True)
for i, violin in enumerate(ax.findobj(mpl.collections.PolyCollection)):
    if i % 2:
        violin.set_hatch("//")

ax.legend_.findobj(mpl.patches.Rectangle)[1].set_hatch("///")

image

This didn't work completely for me, but instead I used:
[c for c in ax.get_children() if isinstance(c, matplotlib.patches.Rectangle)][1].set_hatch("//")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants