question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add split palette and hatching to violinplot?

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
mwaskomcommented, 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

0reactions
luyang-ai4medcommented, 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(“//”)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Control the colors in seaborn violinplots - Python Graph Gallery
Using a color palette. Simply set the 'palette' parameter in the violinplot function. Doing so can add information on the groups order for...
Read more >
Violin plot customization — Matplotlib 3.6.2 documentation
This example demonstrates how to fully customize violin plots. The first plot shows the default style by providing only the data. The second...
Read more >
How to add hatches on seaborn violin plot? - Stack Overflow
IIUC: import seaborn as sns import matplotlib as mpl tips = sns.load_dataset("tips") hatch = ['/','\\','+','o'] ihatch = iter(hatch) ax ...
Read more >
seaborn.violinplot — seaborn 0.12.1 documentation - PyData |
If point or stick , show each underlying datapoint. Using None will draw unadorned violins. splitbool, optional. When using hue nesting with a...
Read more >
Exploring Data Visualisation using Matplotlib and Seaborn
The aim of Seaborn is to provide high-level commands to create a variety of plot ... sns.violinplot(data=df3,palette='rocket') plt.show().
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found