How do I plot two countplot graphs side by side in seaborn?

user517696 picture user517696 · Mar 31, 2017 · Viewed 45.9k times · Source

I am trying to plot two countplots showing the counts of batting and bowling. I tried the following code:

l=['batting_team','bowling_team']
for i in l:
    sns.countplot(high_scores[i])
    mlt.show()

But by using this , I am getting two plots one below the other. How can i make them order side by side?

Answer

Robbie picture Robbie · Mar 31, 2017

Something like this:

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

batData = ['a','b','c','a','c']
bowlData = ['b','a','d','d','a']

df=pd.DataFrame()
df['batting']=batData
df['bowling']=bowlData


fig, ax =plt.subplots(1,2)
sns.countplot(df['batting'], ax=ax[0])
sns.countplot(df['bowling'], ax=ax[1])
fig.show()

enter image description here

The idea is to specify the subplots in the figure - there are numerous ways to do this but the above will work fine.