Python_newbie
Python_newbie

Reputation: 111

Python Side by side box plots after groupby in Matplotlib

Need to do a group by and print 2 boxplots side by side. In the example below I need to plot boxplots for column A by grouping its values by column B.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(10,1), columns=['A'])
df['B'] = [1, 2, 1, 1, 1, 2, 1, 2, 2, 1]
for n, grp in df.groupby('B'):
   plt.boxplot(x='A',data=grp)

Current outputenter image description here

desired output - Something Like: enter image description here

Upvotes: 1

Views: 3499

Answers (1)

JohanC
JohanC

Reputation: 80339

You could add a position to the call to plt.boxplot() to avoid that the boxplot will be drawn twice at the same position:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(10, 1), columns=['A'])
df['B'] = [1, 2, 1, 1, 1, 2, 1, 2, 2, 1]
for n, grp in df.groupby('B'):
    plt.boxplot(x='A', data=grp, positions=[n])
plt.xticks([1, 2], ['Label 1', 'Label 2'])
plt.show()

boxplot from groupby

Or you could use seaborn let the grouping. You can replace the numbers in the column to have labels.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame(np.random.rand(10, 1), columns=['A'])
df['B'] = [1, 2, 1, 1, 1, 2, 1, 2, 2, 1]
df['B'] = df['B'].replace({1: 'Label 1', 2: 'Label 2'})
sns.boxplot(data=df, x='B', y='A' )
plt.show()

seaborn boxplot

Upvotes: 2

Related Questions