Reputation: 111
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)
desired output - Something Like:
Upvotes: 1
Views: 3499
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()
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()
Upvotes: 2