从具有多索引列的Pandas DataFrame创建箱线图

时间:2019-07-04 13:37:36

标签: pandas boxplot multi-index

我有带有多索引列(alpha,接收器,运行)的DataFrame。

我有10个跑步,多个alpha和2个接收器。

我想创建一个箱形图,其中包括10个运行中每个alpha值的两个框(每个接收器一个-rec1,rec2)。

类似这样的事情: desired boxplot figure


repetitions = vectors_toPlot.repetition.unique()
alphas = [0.02, 0.03] # this is example, final version will have more values
receivers = ["rec1", "rec2"]
index = pd.MultiIndex.from_product(iterables, names=['alphas', 'receiver', 'run'])

multiDf = pd.DataFrame(columns=index)
# fill it with values

print(multiDf.head())

alphas          0.02                                                  \
receiver        rec1                                                   
run              0.0         1.0         2.0         3.0         4.0   
0         11744000.0  11744000.0  11744000.0  11744000.0  11744000.0   
1         11744000.0  11744000.0  11744000.0  11744000.0  11744000.0   
2         12331200.0  12331200.0  12331200.0  12331200.0  12331200.0   
3         12624800.0  12624800.0  12624800.0  12624800.0  12624800.0   
4         12331200.0  12331200.0  12331200.0  12331200.0  12331200.0   

我尝试了df.boxplot()bycolumns的各种组合,但我无法理解。

1 个答案:

答案 0 :(得分:0)

您可能需要sns' boxplot

# set up the index
alphas = [0.02, 0.03] # this is example, final version will have more values
receivers = ["rec1", "rec2"]
runs = np.arange(4)
index = pd.MultiIndex.from_product([alphas, receivers, runs], names=['alphas', 'receiver', 'run'])

# toy data
np.random.seed(1)
df = pd.DataFrame(np.random.uniform(0,1, (10,len(index))), columns=index)

# plot
sns.boxplot(x='alphas', y=0, hue='receiver', data=df.unstack().reset_index())

输出

enter image description here