为什么这段代码不能用标题保存我的数字?

时间:2015-07-18 18:43:07

标签: python matplotlib

我使用以下代码生成一些数字:

def boxplot_data(self,parameters_file,figure_title):
    data = pandas.read_csv(parameters_file)
    header = data.keys()
    number_of_full_subplots = len(header)/16
    remainder = len(header)-(16*number_of_full_subplots)
    try:
        for i in range(number_of_full_subplots+1):
            fig =plt.figure(i)
            txt = fig.suptitle(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots),fontsize='20')
            txt.set_text(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots))
            for j in range(16):
                plt.ioff()
                plt.subplot(4,4,j)
                plt.boxplot(data[header[16*i+j]])
                plt.xlabel('')  
                mng=plt.get_current_fig_manager()
                mng.window.showMaximized()                    
                plt.savefig(str(i)+'.png',bbox_inches='tight',orientation='landscape')
                plt.close(fig)                    
                plt.ion()
    except IndexError:
        txt = fig.suptitle(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots),fontsize='20')
        txt.set_text(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots))
        print '{} full figures were created and 1 partially filled \
        figure containing {} subplots'.format(number_of_full_subplots,remainder)

这会以正确格式化的方式生成并保存数据,但无论我做什么,代码似乎都会绕过fig.suptitle行,因此我无法给出我的数字标题。抱歉,如果看起来这个函数中有很多内容我还没有解释,但有没有人解释为什么这段代码拒绝给出我的数字标题?

1 个答案:

答案 0 :(得分:3)

您的问题不是suptitle被绕过,而是您永远不会保存您拨打suptitle的数字。您对savefig的所有调用都在内循环内,因此仅保存子图。如果在代码运行时打开png文件,您实际上可以看到这种情况 - 您会看到每个16个子轴都是逐个添加的。

您的代码看起来不必要复杂。例如,我认为您不需要使用ionioff。这是一个简单的例子,说明如何做我认为你想要的,然后翻译你的代码以适应它(显然我无法测试,因为我没有你的数据)

import matplotlib.pyplot as plt
test_y=range(10)
test_x=[8,13,59,8,81,2,5,6,2,3]

def subplotsave_test():
    for i in range(5):
        fig = plt.figure(i)
        txt = fig.suptitle('Page '+str(i)+' of '+str(5),fontsize='20')
        for j in range(16):
            plt.subplot(4,4,j+1)
            plt.plot(test_y,test_x)          
        plt.savefig(str(i)+'.png',bbox_inches='tight',orientation='landscape')

if __name__ == '__main__':                
    subplotsave_test()

我找到的一个提示适用于我 - 在你打算保存图形的地方做一个plt.show()并确保它看起来像你想要的那样然后 替换 plt.savefig()

打电话

您的功能的可能翻译

def boxplot_data(self,parameters_file,figure_title):
    data = pandas.read_csv(parameters_file)
    header = data.keys()
    number_of_full_subplots = len(header)/16
    remainder = len(header)-(16*number_of_full_subplots)
    for i in range(number_of_full_subplots+1)
        fig =plt.figure(i)
        fig.suptitle(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots),fontsize='20')
        for j in range(16):
            plt.subplot(4,4,j+1)
            if 16*i + j < len(header):
                plt.boxplot(data[header[16*i+j]])
                plt.xlabel('')    
                #You might want the showMaximized() call here - does nothing
                #on my machine but YMMV
            else:
                print '{} full figures were created and 1 partially filled \
                figure containing {} subplots'.format(number_of_full_subplots,remainder)
                break                
        plt.savefig(str(i)+'.png',bbox_inches='tight',orientation='landscape')
        plt.close(fig)