我的目标是生成一个箱形图,以可视化Treat1,treat2,treat3和Treat4中值的分布。
我可以去找一张图表,以根据我想要的东西 How to add group labels for bar charts in matplotlib?
但是,我的要求是让箱形图查看每个对待组的均值和离群值之间的分布。 我再次粘贴代码,该代码基于stackoverflow代码https://stackoverflow.com/users/2846871/varicus
生成条形图df = df2.groupby(['Group','Category','Day ']).sum()
fig = plt.figure(figsize=(20,8))
ax = fig.add_subplot(111)
df.plot(kind='bar',stacked=False,ax=fig.gca())
labels = ['' for item in ax.get_xticklabels()]
ax.set_xticklabels(labels)
ax.set_xlabel('')
label_group_bar_table(ax, df)
fig.subplots_adjust(bottom=.1*df.index.nlevels)
plt.show()
生成Boxplot而不是每个条形的最佳方法是什么。
答案 0 :(得分:0)
这可能就是您要寻找的。
# I create a dataframe similar to yours for others to give other solutions.
group = [1,1,1,1,2,2,4,4, 2, 2, 3, 2, 2, 4, 4, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1,1]
cateogry = [0,0,1,1,0,0,0,0,1,1,1,1,2,2,2,2,3,3,0,0,1,1,0,0,2,2,2]
Treat1 = np.random.randint(0, 100, size= len(group))
Treat2 = np.random.randint(0, 100, size= len(group))
Treat3 = np.random.randint(0, 100, size= len(group))
Treat4 = np.random.randint(0, 100, size= len(group))
df = pd.DataFrame.from_dict({'Group': group, "Category": cateogry, "Treat1": Treat1,"Treat2": Treat2,"Treat3": Treat3,"Treat4": Treat4, })
f = df.boxplot(by = ['Group','Category'],figsize = (12,8))
答案 1 :(得分:0)
我添加了一部分代码来完成这项工作,但是我希望每个类别都有自己的脸色。我似乎无法通过图形区分类别。
fig, ax = plt.subplots(figsize = (20,8))
#Note showfliers=False is more readable, but requires a recent version iirc
bp = df_Dummy.boxplot(by = ['Group','Category','Day '],ax=ax,
sym='',rot=90,return_type='dict',patch_artist=False)
[[item.set_linewidth(2) for item in bp[key]['boxes']] for key in bp.keys()]
[[item.set_linewidth(2) for item in bp[key]['fliers']] for key in bp.keys()]
[[item.set_linewidth(2) for item in bp[key]['medians']] for key in bp.keys()]
[[item.set_linewidth(2) for item in bp[key]['means']] for key in bp.keys()]
[[item.set_linewidth(2) for item in bp[key]['whiskers']] for key in bp.keys()]
[[item.set_linewidth(2) for item in bp[key]['caps']] for key in bp.keys()]
colors = ['pink', 'lightblue', 'lightgreen','yellow']
[[item.set_color in zip(colors) for item in bp[key]['boxes']] for key in bp.keys()]
# seems to have no effect
[[item.set_color('b') for item in bp[key]['fliers']] for key in bp.keys()]
[[item.set_color('m') for item in bp[key]['medians']] for key in bp.keys()]
[[item.set_markerfacecolor('k') for item in bp[key]['means']] for key in bp.keys()]
[[item.set_color('c') for item in bp[key]['whiskers']] for key in bp.keys()]
[[item.set_color('y') for item in bp[key]['caps']] for key in bp.keys()]
ax.margins(y=0.05)