首页 > 解决方案 > 带有seaborn的for循环不显示所有图

问题描述

我一直在尝试使用 for 循环和 seaborn 绘制多个图表。尝试了不同的方法(使用子图并尝试按顺序显示它们),但我无法设法显示所有图表(我取得的最好成绩是绘制列表中的最后一个)。这是我尝试过的两种方法:

fig, ax = plt.subplots(1, 3, sharex = True) #这里只硬编码 thre 3(要绘制的切片器数量)用于测试

for i, col in enumerate(slicers): 
  plt.sca(ax[i])
  ax[i] = sns.catplot(x = 'seq', kind = 'count', hue = col
  , order = dfFirst['seq'].value_counts().index, height=6, aspect=11.7/6
  , data = dfFirst) # distribution.set_xticklabels(rotation=65,                                    horizontalalignment='right')
display(fig)

已经尝试过 plt.sca(ax[i]) 和 ax[i] = sns.catplot 之间的所有组合(如示例中所示,一次激活一个),但显示时 fig 始终显示为空。此外,我尝试使用以下方法按顺序显示数字:

for i, col in enumerate(slicers):
  plt.figure(i)
  sns.catplot(x = 'seq', kind = 'count', hue = col
  , order = dfFirst['seq'].value_counts().index, height=6, aspect=11.7/6
  , data = dfFirst) # distribution.set_xticklabels(rotation=65,   horizontalalignment='right')
  display(figure)

标签: pythonfor-loopseaborndatabricks

解决方案


catplot产生自己的形象。请参阅使用 matplotlib 面向对象的接口使用 seaborn 进行绘图

因此,这里只是

for whatever:
    sns.catplot(...)

plt.show()

推荐阅读