如何在seaborn中并排绘制两个countplot图?

2024-05-23 14:34:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着画出两个显示击球和保龄球的计数图。我尝试了以下代码:

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

但是通过这个,我得到了两个图,一个在另一个下面。我怎样才能让他们同时点餐?


Tags: 代码inforshowteam计数highsns
2条回答

像这样的:

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

我们的想法是在图中指定子块-有很多方法可以做到这一点,但是上面的方法可以很好地工作。

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

相关问题 更多 >