添加第二个x_axis会覆盖标题

时间:2015-07-27 13:53:12

标签: python python-2.7 matplotlib

如何移动子图的标题,使第二轴不写在标题的顶部上,而是低于它?这就是它的样子

enter image description here

以下是相关的代码片段:

    # fig is a subplotAxes object
    fig.set_ylabel('Altitude (km)')
    fig.set_xlabel('Time')   
    fig.get_xaxis().set_major_formatter(mpl.dates.DateFormatter('%H:%M:%S'))

    cbar_label = 'Total Attenuated Backscatter 532nm (km$^{-1}$ sr$^{-1}$)'
    cbar = pfig.colorbar(im)
    cbar.set_label(cbar_label)

    # create second axis
    ax2 = fig.twiny()
    ax2.set_label('Latitude')
    ax2.set_xlim(latitude[0], latitude[-1])

    # set title, but title is overwritten
    fig.set_title("Averaged 532 nm Total Attenuated Backscatter")

我不知道这可能是我的数字大小或简单格式错误的原因。我不想扩展我的图形大小,因为这个图存在于Tkinter窗口

3 个答案:

答案 0 :(得分:2)

由于你的标题适用于整个数字而不仅仅是子图,@GyörgySolymosi的答案可能是要走的路(而不是添加一个新行,这是一种黑客行为)。

或者,您可以将标题文字重新定位到您想要的位置,例如:提高5%:

title = fig.set_title("Flying monkey density")
title_xy = title.get_position()
title.set_position([title_xy[0], title_xy[1]*1.05])

答案 1 :(得分:1)

尝试fig.suptitle("Averaged 532 nm Total Attenuated Backscatter")

答案 2 :(得分:1)

这种解决方案方式比我想象的更简单,我感到愚蠢。我需要做的就是在标题的末尾添加\n

 fig.set_title("Averaged 532 nm Total Attenuated Backscatter\n")

enter image description here