问题描述
我有一个包含两列Q和S的数据集.下面显示了它的一个示例:
I have a dataset with two columns Q and S. The following shows a sample of it:
df = pd.DataFrame(np.array([[1,40], [2, 51], [3, 59], [4, 10],
[5, 30], [7, 20], [9, 21], [13, 30], [15, 70], [2, 81], [3, 85],
[4, 67], [9, 120], [2, 481], [12, 51], [16, 120], [8, 9], [14, 15],
[11, 7], [12, 110], [6, 4], [9, 220], [1, 40], [2, 15], [14, 82], [7, 50]]),columns=['Q', 'S'])
基于"S"列中的值,我定义了三组:
Based on values in column "S" I have defined three groups:
Group_1A = df[(df['S'] >= 0) & (df['S'] <= 3)]
Group_2A = df[(df['S'] >= 4) & (df['S'] <= 8)]
Group_3A = df[(df['S'] >= 9) & (df['S'] <= 16)]
此外,我还为Q"列定义了三个组:
In addition I have also defined three groups for column "Q":
Group_1B = df[(df['Q'] >= 0) & (df['Q'] <= 10)]
Group_2B = df[(df['Q'] >= 11) & (df['Q'] <= 50)]
Group_3B = df[(df['Q'] >= 51) & (df['Q'] <= 481)]
我需要计算出每个组的 S 分布,并且我需要绘制一个包含所有 9 个箱线图的图.
I need to figure out the distribution of S for each group and I need to draw a plot that includes all 9 boxplots.
最终图应如下图所示:
The final plot should be sth like the following image:
我可以绘制每个单独的箱线图,但是我不知道如何在一个图中显示所有箱线图.例如,我可以绘制 Group_1A & 的箱线图.Group_1B使用以下代码:
I can plot each individual boxplot, however I have no idea how can I show all of them in one plot.For example I can plot the boxplot of Group_1A & Group_1B using the following code:
df_B1 = df[df['Q']<=10.0]
df_A1_B1 = df_B1[(df_B1['S'] >= 0) & (df_B1['S'] <= 3)]
fig, ax = plt.subplots()
ax = sns.boxplot(x="S",y="Q", data=df_A1_B1,ax=ax)#,order=order)
但是,这仅提供了我需要的 9 个箱线图中的 1 个.
However, this only gives me 1 out of 9 boxplots that I need.
我想知道是否有人可以帮助我.
I am wondering if anyone can help me.
预先感谢
推荐答案
我刚刚发现我需要使用 matplolib.subplots.
I just found that I need to use matplolib.subplots.
这篇关于在一个图中组合箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!