Comments (7)
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.
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.
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.
Oh, I get it. Thank you for answering me so quickly. Appreciate!
from seaborn.
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()
But the legend did not get the hatch style. Is there any way to do that?
from seaborn.
You can modify those artists too, i think it's something like ax.get_legend().legend_handles
from seaborn.
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()
Thank you again for your help!
from seaborn.
Related Issues (20)
- TypeError: Cannot interpret 'Float64Dtype()' as a data type with pandas extension types HOT 2
- Consider adding mpl.Figure as an optional argument to subplot functions / classes. HOT 1
- AttributeError: module 'numpy' has no attribute 'typeDict'
- categorical plots - unused categories mess up element spacing and width HOT 4
- Customizing context or style as in seaborn as in matplotlib? HOT 1
- Introducing `seaborn_objects_recipes` Library HOT 2
- secondary y axis is messing up the y-axis labels HOT 1
- Adding precomputed errorbars to pointplot HOT 1
- Seaborn with scanpy and statannotations HOT 1
- Linear regression constrained to pass through origin HOT 2
- Dual axis plot broken in 0.13.2 (vs. 0.12.2) HOT 2
- Controlling color of both line and bar at same time using object interface HOT 4
- How to hide legend while using seaborn.objects? HOT 1
- Cannot plot certain data with kdeplot, warning that dataset has 0 variance which is not. HOT 1
- Older Versions of Seaborn Incompatible with Matplotlib 3.9.x HOT 3
- TypeError with seaborn.kdeplot when using fill=True and categorical hue HOT 3
- ModuleNotFoundError: No module named 'seaborn' HOT 1
- `so.Range()` cannot be "overplotted" by an additional mark added with so.Plot.add() HOT 1
- NameError: name 'FloatSlider' is not defined when using widgets.py without ipywidgets installed HOT 1
- Use Arrow PyCapsule Interface instead of Dataframe Interchange Protocol HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from seaborn.