Seaborn: Boxplot hatches

Created on 14 Jun 2018  路  5Comments  路  Source: mwaskom/seaborn

Is there a way to add hatches in boxes produced with seaborn boxplot? I am thinking of something similar to this, but for boxplots.

Many thanks

Most helpful comment

Apologies, I was confused due to the different way that patches are looped over in grouped barplots vs. grouped boxplots. In grouped barplots, patches are looped over one category at a time, e.g., in the plot below, blue bars first, then green bars, whereas in grouped boxplots they are looped over from left to right, e.g., blue box, green box, blue box and so on.

Still there is one tiny difference between barplots and boxplots, which has to do with the legend (see example below), but I am not sure whether this is related to seaborn or matplotlib.

from itertools import cycle

from matplotlib import pyplot as plt
import seaborn as sns; sns.set()

# Load dataset
tips = sns.load_dataset('tips')

# Define some hatches
hatches = cycle(['///', 'x'])

# Barplot
ax = sns.barplot(x="day", y="total_bill", hue="sex", data=tips)
num_locations = len(tips.day.unique())
for i, patch in enumerate(ax.patches):
    # Blue bars first, then green bars
    if i % num_locations == 0:
        hatch = next(hatches)
    patch.set_hatch(hatch)

ax.legend(loc='best')
plt.show(

barplot

from itertools import cycle

from matplotlib import pyplot as plt
import seaborn as sns; sns.set()

# Load dataset
tips = sns.load_dataset('tips')

# Define some hatches
hatches = cycle(['///', 'x'])

# Boxplot
ax = sns.boxplot(x="day", y="total_bill", hue="sex", data=tips)
for i, patch in enumerate(ax.artists):
    # Boxes from left to right
    hatch = next(hatches)
    patch.set_hatch(hatch)

ax.legend(loc='best')
plt.show()

boxplot

Note how the legends are different in the example above.

All 5 comments

Have you tried that approach and found it not to work? This might be a better question for StackOverflow. There's no user-facing seaborn API for varying the hatches.

Yes, I tried it and it didn't work.

I will try stackoverflow, thanks anyway!

What didn't work?

This works for me?

ax = sns.boxplot(x="day", y="total_bill", data=tips)
hatches = ["/", "o", "*", "\\"]
for hatch, patch in zip(hatches, ax.artists):
    patch.set_hatch(hatch)

image

Apologies, I was confused due to the different way that patches are looped over in grouped barplots vs. grouped boxplots. In grouped barplots, patches are looped over one category at a time, e.g., in the plot below, blue bars first, then green bars, whereas in grouped boxplots they are looped over from left to right, e.g., blue box, green box, blue box and so on.

Still there is one tiny difference between barplots and boxplots, which has to do with the legend (see example below), but I am not sure whether this is related to seaborn or matplotlib.

from itertools import cycle

from matplotlib import pyplot as plt
import seaborn as sns; sns.set()

# Load dataset
tips = sns.load_dataset('tips')

# Define some hatches
hatches = cycle(['///', 'x'])

# Barplot
ax = sns.barplot(x="day", y="total_bill", hue="sex", data=tips)
num_locations = len(tips.day.unique())
for i, patch in enumerate(ax.patches):
    # Blue bars first, then green bars
    if i % num_locations == 0:
        hatch = next(hatches)
    patch.set_hatch(hatch)

ax.legend(loc='best')
plt.show(

barplot

from itertools import cycle

from matplotlib import pyplot as plt
import seaborn as sns; sns.set()

# Load dataset
tips = sns.load_dataset('tips')

# Define some hatches
hatches = cycle(['///', 'x'])

# Boxplot
ax = sns.boxplot(x="day", y="total_bill", hue="sex", data=tips)
for i, patch in enumerate(ax.artists):
    # Boxes from left to right
    hatch = next(hatches)
    patch.set_hatch(hatch)

ax.legend(loc='best')
plt.show()

boxplot

Note how the legends are different in the example above.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sofiatti picture sofiatti  路  4Comments

bondarevts picture bondarevts  路  3Comments

vinay-jayaram picture vinay-jayaram  路  3Comments

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments

chanshing picture chanshing  路  3Comments