首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >海航的boxplot+swarmplot: x的不同颜色,颜色不同的符号

海航的boxplot+swarmplot: x的不同颜色,颜色不同的符号
EN

Stack Overflow用户
提问于 2016-11-21 13:22:40
回答 2查看 6.8K关注 0票数 5

我正在尝试使用带有不同x组的海运和附加的hues生成一个盒图。请参阅以下代码:

代码语言:javascript
复制
tips = sns.load_dataset("tips")

sns.stripplot(x="day", y="total_bill", hue="smoker",
              data=tips, jitter=True,
              palette="Set2", dodge=True,linewidth=1,edgecolor='gray')

sns.boxplot(x="day", y="total_bill", hue="smoker",
            data=tips,palette="Set2",fliersize=0)

我希望每个x盒图(在本例中,每天)都是不同的颜色,而每个hue (在本例中,是吸烟者/非吸烟者)在群集图上用不同的符号表示。

我试着玩palette的论点,但没有得到我想要的。我还试图直接使用artists,但是更改盒图的facecolor也会因为某些原因而更改edgecolor,而且我也不知道如何更改群集图上的符号。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-20 20:15:43

我意识到,when answering this question,我从来没有为这个问题提出过我自己的解决方案,尽管我在一段时间前就已经黑了一些东西。

代码语言:javascript
复制
# From itertools' receipes https://docs.python.org/3/library/itertools.html#itertools-recipes
from itertools import zip_longest
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)


fig, ax = plt.subplots()
# dummy plots, just to get the Path objects
a = ax.scatter([1,2],[3,4], marker='s')
b = ax.scatter([1,2],[3,4], marker='^')
square_mk, = a.get_paths()
triangle_up_mk, = b.get_paths()
a.remove()
b.remove()

sns.swarmplot(x="day", y="total_bill", hue="smoker", data=tips, dodge=True, size=6, lw=2, edgecolor='k')
swarm_cols = ax.collections

sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, fliersize=0)
box_cols = ax.artists

ax.legend_.remove()

N_cats = len(np.unique(tips.day))
N_hues = len(np.unique(tips.smoker))
print(N_cats,N_hues)

pastels = matplotlib.cm.get_cmap('Pastel1')
cat_colors =  [pastels(x) for x in np.linspace(0,1,N_cats)]
hue_markers = [square_mk, triangle_up_mk]

for boxes,color in zip(grouper(box_cols, N_hues),cat_colors):
    for box in boxes:
        box.set_facecolor(color)
for swarms,color in zip(grouper(swarm_cols, N_hues), cat_colors):
    for swarm,marker in zip(swarms,hue_markers):
        print(swarm, len(swarm.get_offsets()))
        swarm.set_paths([marker])
        swarm.set_facecolors([color])
        swarm.set_linewidths([1.])
        swarm.set_edgecolors(['xkcd:dark grey'])

# recreate legend
for swarm,marker in zip(swarm_cols[-2:],hue_markers):
    print(swarm, len(swarm.get_offsets()))
    swarm.set_paths([marker])
    swarm.set_facecolors(["none"])
    swarm.set_linewidths([1.])
    swarm.set_edgecolors(['xkcd:dark grey'])
ax.legend(swarm_cols[-2:],np.unique(tips.smoker))

票数 3
EN

Stack Overflow用户

发布于 2017-07-12 09:05:27

对于你问题的着色部分:边缘颜色和面部颜色都可以通过艺术家单独指定。

代码语言:javascript
复制
ax = sns.boxplot(x="day", y="total_bill", hue="smoker",
            data=tips,fliersize=0)

colors = sns.color_palette('husl',n_colors=4)
colors = sp.repeat(colors,2,axis=0)

for artist,color in zip(ax.artists,colors):
    artist.set_facecolor(color)
    artist.set_edgecolor(color)

至于脱衣舞戏:我也找不到一种方法来碰碰运气。如果您想指定它们的颜色等,可以通过[pc for pc in ax.get_children() if type(pc) == PathCollection]单独获取它们.

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

https://stackoverflow.com/questions/40721242

复制
相关文章

相似问题

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