我创建了一个2x2的网格规格,并尝试在第二行绘制catplot,如下所示:
fig = plt.figure(figsize=(10,5), constrained_layout=True)
gs = GridSpec(nrows=2, ncols=2, figure=fig)
# Chart 1
ax1 = fig.add_subplot(gs[0,0])
ax1=sns.countplot(x='product', data = df) #Countplot
plt.title('Product sales', fontweight='bold', fontsize = 8)
plt.ylabel('Count', fontsize = 7)
plt.xlabel('Product', fontsize = 7)
# Chart 2
ax2 = fig.add_subplot(gs[0,1])
ax2= sns.countplot(x='maritalstatus', data = df) #Countplot
plt.title('Marital status of customers', fontweight='bold', fontsize = 8)
plt.ylabel('', fontsize = 7)
plt.xlabel('Marital status', fontsize = 7)
# chart 3
ax2 = fig.add_subplot(gs[1,:])
ax3 = sns.catplot(x = 'product', hue = "gender", col = "maritalstatus", data = df, kind = 'count')
plt.show()但第二行不是由catplot绘制的,而是显示在空白图形的下方。
输出:

发布于 2021-07-22 23:08:30
不幸的是,catplot是一个图形级别的接口,而不是轴级接口,所以您不能用这种方式来绘制它。这是其他图形级接口(如displot )的常见问题,我发现的解决方法是单独使用底层组件(特别是对于displot,它是kdeplot和直方图,对于catplot,您必须查看源代码)。
您可以通过观察接口是否在其调用中接受ax参数来区分哪些接口是图或轴级别的。在您的例子中,您可以浏览the documentation以查看支持哪些绘图,还可以浏览the source code以了解实现细节。
https://stackoverflow.com/questions/68487129
复制相似问题