修改图形布局后,将suptitle置于中心位置

时间:2015-11-05 10:53:55

标签: python-2.7 matplotlib figure

我有一个包含2x2子图的图。顶部和底部子图具有相同的图例,我将其固定到左侧修改我的全局图布局,如下所示:

enter image description here

但我的头衔不再集中!在使用此命令进行所有修改后,我完成了它:

f.suptitle('Background Efficiency', fontsize=20)

有人知道如何让它居中吗?我尝试了tight_layout()方法,但它修改了所有的布局而没有做我想做的事。

我正在绘制所有内容的功能:

def customplot( plots ):
    i=0
    titles=plots.keys()
    figs={}
    # Create all the figures needed (1 per test efficiency)
    while i<len(plots):
            # Create the figure that will contain 4 subplots
            f, axarr = plt.subplots(2, 2)

            # Get the figures data to plot from the 'plots' dictionary
            mywindow=plots[titles[i]]

            # Get curves names (either 'k' or 'filtering') from the 'mywindow' dictionary
            curves_names=mywindow.keys()
            # Get xaxis varying parameter (either 'ratios' or 'dbsizes') from the 'mywindow' dictionary
            x_names=mywindow[curves_names[0]].keys()

            j=0
            while j<len(curves_names):
                    # Get the data to plot in a single subplot from the 'mywindow' dictionary
                    toplot=mywindow[curves_names[j]]
                    k=0
                    while k<len(x_names):
                            # Get the data of a single curve to plot from the 'toplot' dictionary
                            tosubplot=toplot[x_names[k]]

                            # The previous data is a dictionary containing int keys (curves to draw) and strings (xaxis or fixed parameters). We are saving them
                            ints=list()
                            strings=list()
                            ids=tosubplot.keys()
                            l=0
                            while l<len(ids):
                                    if isinstance( ids[l], ( int, long ) ):
                                            ints.append(ids[l])
                                            ints = sorted(set(ints))
                                    else:
                                            strings.append(ids[l])
                                    l+=1
                            # Tuning our subplot
                            if (k==0):
                                    lll='left'
                            else:
                                    lll='right'
                            axarr[j,k].set_title('Plotting ' + curves_names[j] + ' depending on ' + x_names[k], loc=lll)
                            axarr[j,k].grid('on')
                            xxx=tosubplot['x']
                            if(x_names[k]=='ratios'):
                                    minaxis=float(xxx[0]-(xxx[1]-xxx[0]))
                                    maxaxis=float(xxx[len(xxx)-1]+xxx[1]-xxx[0])
                                    axarr[j,k].axis([minaxis,maxaxis,0,1])
                            else:
                                    minaxis=int(xxx[0]-(xxx[1]-xxx[0]))
                                    maxaxis=int(xxx[len(xxx)-1]+xxx[1]-xxx[0])
                                    axarr[j,k].axis([minaxis,maxaxis,0,1])
                            if (k==0):
                                    axarr[j,k].set_ylabel('F-measure of the face recognition')
                            if (j==1 and k==0):
                                    axarr[j,k].set_xlabel('Ratio chosen to tune the recognition')
                            if (j==1 and k==1):
                                    axarr[j,k].set_xlabel('Size of the database')
                            l=0
                            # Plot all the curves in a subplot
                            while l<len(ints):
                                    # Create labels for the legend
                                    cstlabel=curves_names[j]
                                    if cstlabel=='k':
                                            cstlabel=cstlabel + ' = ' + str(ints[l])
                                    else:
                                            if ints[l]==0:
                                                    cstlabel='wavelets'
                                            elif ints[l]==1:
                                                    cstlabel='subsampling'
                                            elif ints[l]==2:
                                                    cstlabel='averaging'
                                    # Finally plotting
                                    axarr[j,k].plot(xxx[0:len(tosubplot[ints[l]])], tosubplot[ints[l]], 'o--', label=cstlabel)
                                    l+=1
                            k+=1
                    j+=1
            # Hiding xticks of top subplots and yticks of right subplots
            plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
            plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)

            # Modify position of subplots to adjust the legends
            p=axarr[0,1].get_position()
            p=[p.x0+0.08, p.y0, p.width, p.height]
            axarr[0,1].set_position(p)
            p=axarr[1,1].get_position()
            p=[p.x0+0.08, p.y0, p.width, p.height]
            axarr[1,1].set_position(p)

            # Create the legends
            axarr[0,0].legend(bbox_to_anchor=(1.08,1), loc=2, ncol=1, borderaxespad=0.03, borderpad=1.5, labelspacing=1.5)
            axarr[1,0].legend(bbox_to_anchor=(1.05,1), loc=2, ncol=1, borderaxespad=0.00, borderpad=0.7, labelspacing=1.5)

            # Finally add the title
            f.suptitle(titles[i], fontsize=20)

            f.set_size_inches(18, 8, forward=True)
            # Saving the figure in the return list
            figs[titles[i]]=f
            i+=1
    return figs

0 个答案:

没有答案