Reputation: 105
The follwoing is a sample of the dataset that I have:
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]]),columns=['A', 'B')
Based on the values in column A I have defined three groups:
Group_1A = df[(df['A'] >= 0) & (df['A'] <= 3)]
Group_2A = df[(df['A'] >= 4) & (df['A'] <= 7)]
Group_3A = df[(df['A'] >= 9) & (df['A'] <= 15)]
For column B I also have three groups as follows:
Group_1B = df[(df['B'] >= 10) & (df['B'] <= 30)]
Group_2B = df[(df['B'] >= 40) & (df['B'] <= 60)]
Group_3B = df[(df['B'] >= 70) & (df['B'] <= 100)]
Now I need a plot that shows all 9 box-plots together. Sth like the following picture.
I tried to use seaborn.boxplot and I tried to see if I can plot 9 boxplots individually and then combine them together but it did not work. For example, I tried to define a new data set based on group1A and 1B as follow:
df2 = Group_1A[Group_1A[(df['B'] >= 10) & (df['B'] <= 30)]]
fig, ax = plt.subplots(figsize=(9,9))
ax = sns.boxplot(x="B",y="A", data=df2,ax=ax)#,order=order)
This only gives me one of the boxplots (when A is in range [0-3] and B in range [10-30]).
I am wondering if anyone can help me.
Thanks in advance
Upvotes: 0
Views: 184
Reputation: 40697
Are you trying to draw the distribution of "B" for each of your groups?
This code gives something similar to the drawing that you provided, but I may have misunderstood the question.
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]]),columns=['A', 'B'])
lim_A = [[0,3],[4,7],[9,15]]
lim_B = [[10,30],[40,60],[70,100]]
fig, ax = plt.subplots()
boxes = []
for ypos,a in enumerate(lim_A):
for b in lim_B:
temp = df.loc[(df.A>=a[0])&(df.A<=a[1])&(df.B>=b[0])&(df.B<=b[1])]
boxes.append(ax.boxplot(temp['B'].values, vert=False, positions=[ypos]))
ax.set_ylim(-0.5,(len(lim_A)-1)+0.5)
ax.set_yticks(range(len(lim_A)))
ax.set_yticklabels([f'[{a}–{b}]' for a,b in lim_A])
ax.set_xticks([np.mean(a) for a in lim_B])
ax.set_xticklabels([f'[{a}–{b}]' for a,b in lim_B])
ax.set_xlabel('B')
ax.set_ylabel('A')
Upvotes: 1