首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用sns.boxplot或sns.catplot在盒图中添加哈茨

如何用sns.boxplot或sns.catplot在盒图中添加哈茨
EN

Stack Overflow用户
提问于 2022-06-17 09:02:57
回答 1查看 401关注 0票数 2

我需要把舱口加到一个明确的盒子里。我所拥有的是:

我需要的是这样的东西(用中线):

我试过的是这个代码:

代码语言:javascript
复制
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise, kind="box")
bars = g.axes[0][0].patches
hatches=['//','..','xx','//','..','xx','//','..','xx']
for pat,bar in zip(hatches,bars):
    bar.set_hatch(pat)

这只会产生第一个数字。第3-6行的想法来自于this question。但是,在第3行中获得axes[0][0]的想法来自于this question

因为FacetGrids没有像补丁或容器这样的属性,所以很难将个别图中关于孵化的答案调整到分类图中,所以我无法理解它。

其他复习过的不起作用的问题:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-17 14:35:55

for ax in g.axes.flat:.

  • ax.patches对每个子图/ FacetGrid进行迭代,
  1. 包含matplotlib.patches.Rectanglematplotlib.patches.PathPatch,因此正确的matplotlib.patches.Rectanglematplotlib.patches.PathPatch必须为每个组在每个方面出现所有颜色,否则patcheshatches将不匹配。在这种情况下,可能需要手动或条件代码来正确确定works.

,因此zip(patches, h)

  • Tested in python 3.10**,** pandas 1.4.2**,** matplotlib 3.5.1**,** seaborn 0.11.2

代码语言:javascript
复制
import matplotlib as mpl
import seaborn as sns

# load test data
exercise = sns.load_dataset("exercise")

# plot
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise, col='diet', kind="box")

# hatches must equal the number of hues (3 in this case)
hatches = ['//', '..', 'xx']

# iterate through each subplot / Facet
for ax in g.axes.flat:

    # select the correct patches
    patches = [patch for patch in ax.patches if type(patch) == mpl.patches.PathPatch]
    # the number of patches should be evenly divisible by the number of hatches
    h = hatches * (len(patches) // len(hatches))
    # iterate through the patches for each subplot
    for patch, hatch in zip(patches, h):
        patch.set_hatch(hatch)
        fc = patch.get_facecolor()
        patch.set_edgecolor(fc)
        patch.set_facecolor('none')

  • 添加以下内容,以更改图例.

代码语言:javascript
复制
for lp, hatch in zip(g.legend.get_patches(), hatches):
    lp.set_hatch(hatch)
    fc = lp.get_facecolor()
    lp.set_edgecolor(fc)
    lp.set_facecolor('none')

  • 如果仅使用轴级sns.boxplot,则不需要迭代多个轴.

代码语言:javascript
复制
ax = sns.boxplot(x="time", y="pulse", hue="kind", data=exercise)

# select the correct patches
patches = [patch for patch in ax.patches if type(patch) == mpl.patches.PathPatch]
# the number of patches should be evenly divisible by the number of hatches
h = hatches * (len(patches) // len(hatches))
# iterate through the patches for each subplot
for patch, hatch in zip(patches, h):
    patch.set_hatch(hatch)
    fc = patch.get_facecolor()
    patch.set_edgecolor(fc)
    patch.set_facecolor('none')

l = ax.legend()
    
for lp, hatch in zip(l.get_patches(), hatches):
    lp.set_hatch(hatch)
    fc = lp.get_facecolor()
    lp.set_edgecolor(fc)
    lp.set_facecolor('none')

保留方框情节的面部颜色的patch.set_facecolor('none')

  • Set

  1. 将edgecolor移除为'k' (黑色)而不是fcpatch.set_edgecolor('k').

代码语言:javascript
复制
- Applies to the `sns.catplot` code too.
代码语言:javascript
复制
ax = sns.boxplot(x="time", y="pulse", hue="kind", data=exercise)

# select the correct patches
patches = [patch for patch in ax.patches if type(patch) == mpl.patches.PathPatch]
# the number of patches should be evenly divisible by the number of hatches
h = hatches * (len(patches) // len(hatches))
# iterate through the patches for each subplot
for patch, hatch in zip(patches, h):
    patch.set_hatch(hatch)
    patch.set_edgecolor('k')
    
l = ax.legend()
    
for lp, hatch in zip(l.get_patches(), hatches):
    lp.set_hatch(hatch)
    lp.set_edgecolor('k')

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72656861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档