我正在尝试在Python 2.7中为下面的Pandas数据帧中列E中的每个分类值生成一个箱线图。
A B C D E
0 0.647366 0.317832 0.875353 0.993592 1
1 0.504790 0.041806 0.113889 0.445370 2
2 0.769335 0.120647 0.749565 0.935732 3
3 0.215003 0.497402 0.795033 0.246890 1
4 0.841577 0.211128 0.248779 0.250432 1
5 0.045797 0.710889 0.257784 0.207661 4
6 0.229536 0.094308 0.464018 0.402725 3
7 0.067887 0.591637 0.949509 0.858394 2
8 0.827660 0.348025 0.507488 0.343006 3
9 0.559795 0.820231 0.461300 0.921024 1
我愿意使用Matplotlib或任何其他绘图库来完成这个任务。目前,以上代码可以在一个图中绘制所有类别的数据。以下是生成上述数据并绘制图表的代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# Data
df = pd.DataFrame(np.random.rand(10,4),columns=list('ABCD'))
df['E'] = [1,2,3,1,1,4,3,2,3,1]
# Boxplot
bp = ax.boxplot(df.iloc[:,:-1].values, widths=0.2)
plt.show()
在这个例子中,类别是1、2、3、4。我想在同一图上侧边放置分开的箱线图,只显示类别1和2,并在图例中显示类别名称。
有什么方法可以做到这一点吗?
额外信息:
输出应该类似于here中的第三幅图 - 将“Yes”,“No”替换为“1”,“2”。