Python 如何在一个循环中绘制一组子图,并在外部循环中每次保存子图?

Python 如何在一个循环中绘制一组子图,并在外部循环中每次保存子图?,python,matplotlib,Python,Matplotlib,我想在每次迭代后在一个子批次中绘制两个内部循环的结果,而不关闭它,这将被放置在一个外部循环中,在外部循环的每次迭代后,我想保存这些图。但不幸的是,它只是绘制每个循环中的最后一组数据。我怎么能把它们都画出来呢 shape=['.','s','^'] colors=['r','b','m','c'] for l in range(len(rg)): for ii in range(len(ANISO_POLY)): for

我想在每次迭代后在一个子批次中绘制两个内部循环的结果,而不关闭它,这将被放置在一个外部循环中,在外部循环的每次迭代后,我想保存这些图。但不幸的是,它只是绘制每个循环中的最后一组数据。我怎么能把它们都画出来呢

     shape=['.','s','^']
     colors=['r','b','m','c']    
     for l in range(len(rg)):
        for ii in range(len(ANISO_POLY)):
            for iii in range(len(ST_INT)):
                rc('text', usetex=True)
                rc('font', family='serif')
                fig = plt.figure(figsize=(5,7.5))
                ax = fig.add_subplot(2,1,1)
                ax.set_xscale('log')
                #m, mean_D1, stdev_D1, mean_D2 and stdev_D2 are generated in the inner loop each time
                ax.errorbar(m, mean_D1, yerr=stdev_D1, color=colors[ii], ecolor=colors[ii], fmt=shape[iii], capsize=0, elinewidth=1.0,linestyle='dashed' ,label=ANISO_POLY[ii]+','+ST_INT[iii])
                ax.set_xlabel(r'$\Theta$', fontsize=15)
                ax.set_ylabel(r'$D_1^{+}$', fontsize=15)
                fontsize=10
                for tick in ax.xaxis.get_major_ticks():
                    tick.label1.set_fontsize(fontsize)
                for tick in ax.yaxis.get_major_ticks():
                    tick.label1.set_fontsize(fontsize)            
                ax = fig.add_subplot(2,1,2)
                ax.set_xscale('log')
                ax.errorbar(m, mean_D2, yerr=stdev_D2, color=colors[ii], ecolor=colors[ii], fmt=shape[iii], capsize=0, elinewidth=1.0,linestyle='dashed',label=ANISO_POLY[ii]+','+ST_INT[iii] )
                ax.set_xlabel(r'$\Theta$', fontsize=15)
                ax.set_ylabel(r'$D_2^{+}$', fontsize=15)
                fontsize=10
                for tick in ax.xaxis.get_major_ticks():
                    tick.label1.set_fontsize(fontsize)
                for tick in ax.yaxis.get_major_ticks():
                    tick.label1.set_fontsize(fontsize)
        lg=ax.legend(numpoints=1,loc="upper right", ncol=1,fontsize=8)
        lg.draw_frame(False)
        plotfile='A226_R.ellip'+rg[l]+'.pdf'
        plt.savefig(plotfile, dpi=50, bbox_inches='tight')
        plt.close()

这是我的密码,我不知道我哪里出错了

如果我试图保存的每个图形的设置都保留在外部循环中,则绘图不会每次更新

colors = cm.rainbow(np.linspace(0, 1, 12))
for l in range(len(rg)):
    color_count=0
    rc('text', usetex=True)
    rc('font', family='serif')
    fig = plt.figure(figsize=(5,7.5))
    ax1 = fig.add_subplot(2,1,1)
    ax1.set_xscale('log')
    x0,x1 = ax1.get_xlim()
    y0,y1 =ax1.set_ylim(-2e-4,2e-4)
    ax1.set_yticks((-2e-4,-1.5e-4,-1e-4,-0.5e-4,0.0,0.5e-4,1e-4,1.5e-4,2e-4))
    ax1.set_yticklabels([r'$-2\times 10^{-4}$',r'$-1.5\times 10^{-4}$' ,r'$-1\times 10^{-4}$',r'$-0.5\times 10^{-4}$' , r'$0.0$', r'$0.5\times 10^{-4}$',r'$1\times 10^{-4}$',r'$1.5\times 10^{-4}$',r'$2\times 10^{-4}$'])
    ax1.set_xlabel(r'$\Theta$', fontsize=15)
    ax1.set_ylabel(r'$D_1^{+}$', fontsize=15)
    fontsize=10
    for tick in ax1.xaxis.get_major_ticks():
        tick.label1.set_fontsize(fontsize)
    for tick in ax1.yaxis.get_major_ticks():
        tick.label1.set_fontsize(fontsize)            
    ax1.set_aspect((x1-x0)/(y1-y0))
    ax2 = fig.add_subplot(2,1,2, aspect=1)
    ax2.set_xscale('log')
    ax2.set_ylim(-2e-4,4e-4)
    ax2.set_yticks((-2e-4,-1e-4,0.0,1e-4,2e-4,3e-4,4e-4))
    ax2.set_yticklabels([r'$-2\times 10^{-4}$',r'$-1\times 10^{-4}$' , r'$0.0$', r'$1\times 10^{-4}$',r'$2\times 10^{-4}$',r'$3\times 10^{-4}$',r'$4\times 10^{-4}$'])
    ax2.set_xlabel(r'$\Theta$', fontsize=15)
    ax2.set_ylabel(r'$D_2^{+}$', fontsize=15)
    fontsize=10
    for tick in ax2.xaxis.get_major_ticks():
        tick.label1.set_fontsize(fontsize)
    for tick in ax2.yaxis.get_major_ticks():
        tick.label1.set_fontsize(fontsize)
    for ii in range(len(ANISO_POLY)):
        for iii in range(len(ST_INT)):
            ax1.errorbar(m, mean_D1, yerr=stdev_D1, color=colors[color_count], ecolor=colors[color_count], fmt='.', capsize=0, elinewidth=1.0,linestyle='dashed' ,label='Poly. or.:'+ANISO_POLY[ii]+',St. int.:'+ST_INT[iii])
            ax2.errorbar(m, mean_D2, yerr=stdev_D2, color=colors[color_count], ecolor=colors[color_count], fmt='.', capsize=0, elinewidth=1.0,linestyle='dashed',label='Poly. or.:'+ANISO_POLY[ii]+', St. int.:'+ST_INT[iii])
            color_count+=1
    lg=ax2.legend(numpoints=1,loc=4, ncol=3,fontsize=5)
    lg.draw_frame(False)
    plotfile='A226_R.ellip'+rg[l]+'.pdf'
    plt.savefig(plotfile, dpi=50, bbox_inches='tight')
    plt.close()

你能把这减少到能证明你的问题的最小代码量吗?许多人(包括我自己)不会阅读那么多无关的代码来帮助您解决问题。将所有数据替换为随机数或直线(
np.arange(10)
)。@tcaswell我试图减少代码中无用的部分。我们不需要看到轴标签,不需要生成数据的循环,不需要任何图例代码等。从顶部的文本中,如果使用右侧的滚动条格式化代码,则说明代码太多。而且您的代码没有正确缩进。@根据您的评论,我希望它现在能正常工作!变得更好,不需要轴标签或勾号调整,但需要假数据;)