Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 471
user517696 Avatar asked Mar 31 '17 02:03

user517696


People also ask

How do you plot two Seaborn plots side by side?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.

How do you plot the horizontal Countplot in Seaborn?

To create a horizontal bar chart or countplot in Seaborn, you simply map your categorical variable to the y-axis (instead of the x-axis). When you map the categorical variable to the y-axis, Seaborn will automatically create a horizontal countplot.

Can you use Seaborn with subplots?

In this article, we will explore how to create a subplot or multi-dimensional plot in seaborn, It is a useful approach to draw subplot instances of the same plot on different subsets of your dataset. It allows a viewer to quickly extract a large amount of data about complex information.


2 Answers

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.

like image 179
Robbie Avatar answered Oct 13 '22 21:10

Robbie


import matplotlib.pyplot as plt l=['batting_team', 'bowling_team'] figure, axes = plt.subplots(1, 2) index = 0 for axis in axes:   sns.countplot(high_scores[index])   index = index+1 plt.show() 
like image 45
Ravi Avatar answered Oct 13 '22 21:10

Ravi