Python Matplotlib图标题使用双引号时会与轴标签重叠

我试图用twiny在同一个图上绘制两个单独的数量,如下所示:

fig = figure() ax = fig.add_subplot(111) ax.plot(T, r, 'b-', T, R, 'r-', T, r_geo, 'g-') ax.set_yscale('log') ax.annotate('Approx. sea level', xy=(Planet.T_day*1.3,(Planet.R)/1000), xytext=(Planet.T_day*1.3, Planet.R/1000)) ax.annotate('Geostat. orbit', xy=(Planet.T_day*1.3, r_geo[0]), xytext=(Planet.T_day*1.3, r_geo[0])) ax.set_xlabel('Rotational period (hrs)') ax.set_ylabel('Orbital radius (km), logarithmic') ax.set_title('Orbital charts for ' + Planet.N, horizontalalignment='center', verticalalignment='top') ax2 = ax.twiny() ax2.plot(v,r,'k-') ax2.set_xlabel('Linear speed (ms-1)') show() 

和数据显示正常,但我有问题,数字标题是重叠的辅助X轴上的轴标签,使它几乎不可读(我想在这里张贴图片的例子,但我没有足够高的代表呢)。

我想知道是否有直接将标题直接移动几十个像素的方法,以使图表看上去更漂亮。

我不确定这是否是matplotlib后来版本中的一个新特性,但至less在1.3.1版本中,这很简单:

 plt.title(figure_title, y=1.08) 

这也适用于plt.suptitle() ,但不适用于plt.xlabel()

忘记使用plt.title并将文本直接plt.text 。 一个过分夸大的例子如下:

 import pylab as plt fig = plt.figure(figsize=(5,10)) figure_title = "Normal title" ax1 = plt.subplot(1,2,1) plt.title(figure_title, fontsize = 20) plt.plot([1,2,3],[1,4,9]) figure_title = "Raised title" ax2 = plt.subplot(1,2,2) plt.text(0.5, 1.08, figure_title, horizontalalignment='center', fontsize=20, transform = ax2.transAxes) plt.plot([1,2,3],[1,4,9]) plt.show() 

在这里输入图像说明

 ax.set_title('My Title\n', fontsize="15", color="red") plt.imshow(myfile, origin="upper") 

如果你把'\n'放在标题string后面,就会在标题下面画出图。 这可能也是一个快速的解决scheme。