使用Python在同一图形中绘制两个CATPlot

使用Python在同一图形中绘制两个CATPlot,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我尝试在同一个图形中绘制两个CATPlot。我尝试使用subplot()函数,但没有结果。 下面是我用来一次绘制一个catplot的代码 第一个Catplot fig, axs =plt.subplots(2,1) sns.catplot(x = 'day',y = 'count',data=day_of_month_count, kind ='bar', height = 8 , aspect= 1.5,ax=axs[0]) 第二个Catpl

我尝试在同一个图形中绘制两个CATPlot。我尝试使用subplot()函数,但没有结果。 下面是我用来一次绘制一个catplot的代码

第一个Catplot

fig, axs =plt.subplots(2,1)
sns.catplot(x = 'day',y = 'count',data=day_of_month_count,
            kind ='bar',
            height = 8 , aspect= 1.5,ax=axs[0])

第二个Catplot

fig, axs =plt.subplots(2,1)
sns.catplot(x = 'day',y = 'count',data=day_of_month_count,
            kind ='bar',
            height = 8 , aspect= 1.5,ax=axs[0])
下面是第二个catplot am绘图:

sns.catplot(x = 'day',y = 'count',data=day_of_month_count,
           kind ='bar',
           height = 8 , aspect= 1.5,ax=axs[1])

目标在同一图形中绘制到CATPlot(一个挨着另一个)

我尝试了类似的方法(使用子图),但它不起作用

fig, axs =plt.subplots(2,1)
sns.catplot(x = 'day',y = 'count',data=day_of_month_count,
            kind ='bar',
            height = 8 , aspect= 1.5,ax=axs[0])
sns.catplot(x = 'month',y = 'count',data=month_of_the_year_count,
           kind ='bar',
           height = 8 , aspect= 1.5,ax=axs[1])

还有别的选择吗?解决方案?

首先,彼此相邻需要1行2列。然后,以下方法按预期正常工作

在这里,您必须关闭/隐藏由catplot返回的轴。这可以使用正确的索引和
plt.close来完成。数字的索引/编号从0开始。下面是一个示例答案

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")

fig, axs = plt.subplots(1,2)

sns.catplot(x="time", y="pulse", kind ='bar', data=exercise, ax=axs[0])
sns.catplot(x="time", y="pulse", kind ='bar', data=exercise, ax=axs[1])
plt.close(2)
plt.close(3)

fig.tight_layout()

有些事情不对劲。我正在执行与您相同的代码。在plt.savefig('xxxx.png')末尾添加。我得到的输出是一个有两个轴的图形(但没有CATPLOT)@mouni93你能试着在一个新的终端中复制粘贴我的代码并运行它吗?通过终端执行相同的代码,并在最后添加savefig会得到与你相同的输出。我不知道我的jupyter怎么了。我通过jupyter笔记本(!python test.py)执行脚本,它运行良好。ThanksI否决了这一点,因为这显然是对seaborn catplot函数的滥用。这种变通办法的问题在于,它们会四处传播,为那些不知道其含义的人带来新的问题。稍后,一旦figure level seaborn函数接受
ax
的pass-through参数的错误被修复,它们将再次开始询问代码为何不再工作。相反,人们应该接受这样一个事实,即catplot创建自己的图形,或者使用catplot的
col
参数,或者分别创建两个条形图。@importantanceofbeingernest:感谢您为否决票辩护。谢谢!