Python 在matplotlib中以未知总数迭代添加子批次

Python 在matplotlib中以未知总数迭代添加子批次,python,matplotlib,subplot,Python,Matplotlib,Subplot,我有一组嵌套字典,我正在为它们生成子图。我需要根据字典中的终端答案进行读取和过滤,然后根据字典树上的变量将结果绘制成图表。在某些情况下,树上的变量是用于切片其他数据集的索引: for field in data.keys(): if field.startswith('Header'): for subheading in data.get(field): for index, answer in data.get(field).get(subhe

我有一组嵌套字典,我正在为它们生成子图。我需要根据字典中的终端答案进行读取和过滤,然后根据字典树上的变量将结果绘制成图表。在某些情况下,树上的变量是用于切片其他数据集的索引:

for field in data.keys():
    if field.startswith('Header'):
        for subheading in data.get(field):
            for index, answer in data.get(field).get(subheading).get('Directory').items():
                if answer=='yes':
                   axes.plot(index, seconddataset[subheading]...
我希望为整个循环的每个迭代生成一个单独的子批,最好安排在一个列中(我对其他列有其他用途)。如果我知道从循环中得到的实例总数,那么我可以使用
plt.subplot(rows,columns)
并使用
axes[row,column].plot(…)
索引每个实例。当我尝试
fig.add_subplot
方法时,我不断得到以嵌套方式彼此重叠的子图-子图不会更新其位置以反映随着每次循环迭代而增长的图形


也许这是不好的代码实践,所以我也有一个版本,我只需在迭代字典时计算
'yes'
答案的数量,然后将该数字与上面的
plt.subplot(行、列)
策略一起使用,就可以获取条目的总数。要做到这一点,我必须编写条件嵌套循环,并对其进行第二次迭代(一次获取总循环,然后再次绘制子循环),但这似乎也是低效的

有时候写下你的问题会给你一个答案。我会把我的写在这里,以防有任何评论或类似的问题。当遍历嵌套字典时,除了计算/收集终端
答案
,我还收集稍后用于绘图的变量,将它们附加到列表中

for field in data.keys():
    if field.startswith('Header'):
        for subheading in data.get(field):
            for index, answer in data.get(field).get(subheading).get('Directory').items():
                if answer=='yes':
                    subheading_list.append(subheading)
                    index_list.append(index)

fig,axes = plt.subplots(len(index_list),3)
for i in range (0,len(index_list)):
    axes[i,0].plot(index_list[i], seconddataset[subheading_list[i]]...