Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matplotlib Seaborn';s箱线图+;Swarmlot:x的颜色不同,色调的符号不同_Matplotlib_Boxplot_Seaborn - Fatal编程技术网

Matplotlib Seaborn';s箱线图+;Swarmlot:x的颜色不同,色调的符号不同

Matplotlib Seaborn';s箱线图+;Swarmlot:x的颜色不同,色调的符号不同,matplotlib,boxplot,seaborn,Matplotlib,Boxplot,Seaborn,我正在尝试使用seaborn生成一个具有不同x组和附加色调的箱线图。请参阅此代码: tips = sns.load_dataset("tips") sns.stripplot(x="day", y="total_bill", hue="smoker", data=tips, jitter=True, palette="Set2", split=True,linewidth=1,edgecolor='gray') sns.boxplot(

我正在尝试使用seaborn生成一个具有不同
x
组和附加
色调的箱线图。请参阅此代码:

tips = sns.load_dataset("tips")

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

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

我希望每个
x
箱线图(在本例中,每天)都是不同的颜色,而每个
色调(在本例中,是吸烟者/非吸烟者)在Swarmlot上用不同的符号表示


我试图使用
调色板
参数,但没有得到我想要的。我还试图直接与
艺术家
玩游戏,但由于某种原因,改变箱线图的
面色
也会改变
边色
,我也不知道如何更改Swarmlot上的符号。

至于问题的着色部分:edgecolor和facecolor都可以通过艺术家单独指定

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()(如果类型(pc)=PathCollection]
分别获取它们,如果您想指定它们的颜色等

我意识到,我从来没有为这个问题提出过自己的解决方案,尽管我不久前已经拼凑了一些东西

# 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))

以下是一种在不创建虚拟图形的情况下获取标记的方法: