Giter Site home page Giter Site logo

set_hatch not work in boxplot about seaborn HOT 7 CLOSED

eeeXun avatar eeeXun commented on September 13, 2024
set_hatch not work in boxplot

from seaborn.

Comments (7)

mwaskom avatar mwaskom commented on September 13, 2024

It's possible that matplotlib changed the underlying data model as ax.artists is empty in your example script. But you can access the box patches more directly at ax.patches. Also seaborn v0.13 started packaging the boxplot artists in a container and storing it at ax.containers which is another way of making your post-processing a bit more explicit.

from seaborn.

eeeXun avatar eeeXun commented on September 13, 2024

Thanks. Changing ax.artists to ax.patches works for me.

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.patches):
    # Boxes from left to right
    hatch = next(hatches)
    patch.set_hatch(hatch)

ax.legend()
plt.show()

But what do you mean ax.containers? Changing ax.artists to ax.containers gives the error that BoxPlotContainer has no attribute set_hatch. Do I call it the wrong way?

from seaborn.

mwaskom avatar mwaskom commented on September 13, 2024

Do I call it the wrong way?

Yes — do e.g.

bxp = ax.containers[0]
bxp.boxes
[<matplotlib.patches.PathPatch at 0x12b095a50>,
 <matplotlib.patches.PathPatch at 0x12b0a4950>,
 <matplotlib.patches.PathPatch at 0x12b0602d0>,
 <matplotlib.patches.PathPatch at 0x12b0636d0>]

from seaborn.

eeeXun avatar eeeXun commented on September 13, 2024

Oh, I get it. Thank you for answering me so quickly. Appreciate!

from seaborn.

eeeXun avatar eeeXun commented on September 13, 2024

Hi @mwaskom, after trying again. ax.containers is exactly what I want.

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

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

# Define some hatches
hatches = ['///', 'x']

# Boxplot
ax = sns.boxplot(x="day", y="total_bill", hue="sex", data=tips)
for i in range(2):
    for box in ax.containers[i].boxes:
        box.set_hatch(hatches[i % len(hatches)])

ax.legend()
plt.show()

download

But the legend did not get the hatch style. Is there any way to do that?

from seaborn.

mwaskom avatar mwaskom commented on September 13, 2024

You can modify those artists too, i think it's something like ax.get_legend().legend_handles

from seaborn.

eeeXun avatar eeeXun commented on September 13, 2024
from matplotlib import pyplot as plt
import seaborn as sns; sns.set()

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

# Define some hatches
hatches = ['///', 'x']

# Boxplot
ax = sns.boxplot(x="day", y="total_bill", hue="sex", data=tips)
ax.legend()
for i in range(2):
    for box in ax.containers[i].boxes:
        box.set_hatch(hatches[i % len(hatches)])
    ax.get_legend().legend_handles[i].set_hatch(hatches[i % len(hatches)])

plt.show()

download

Thank you again for your help!

from seaborn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.