Diz*_*ahi 5 matplotlib boxplot seaborn
我正在尝试使用具有不同x组的seaborn和附加hues. 看到这个代码:
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)
Run Code Online (Sandbox Code Playgroud)
我想让每个x箱线图(在这个例子中,每天)是不同的颜色,而每个hue(在这种情况下,吸烟者/非吸烟者)在群图上用不同的符号表示。
我试图玩弄这个palette论点,但没有得到我想要的。我也尝试artists直接使用 ,但是由于某种原因facecolor更改箱线图的 也会更改edgecolor,而且我不知道如何更改 swarmplot 上的符号。
我意识到,在回答这个问题时,我从未对这个问题提出自己的解决方案,尽管我不久前已经将一些东西组合在一起了。
# 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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4826 次 |
| 最近记录: |