Python 如何在 seaborn 中并排绘制两个计数图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43131274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:33:53  来源:igfitidea点击:

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

pythonpandasmatplotlibseaborn

提问by user517696

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?

但是通过使用 this ,我得到了两个图一个在另一个下面。我怎样才能让它们并排订购?

回答by Robbie

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.

这个想法是在图中指定子图 - 有很多方法可以做到这一点,但上述方法可以正常工作。

回答by Ravi G

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()