使用 twiny 时,Python Matplotlib 图形标题与轴标签重叠 [英] Python Matplotlib figure title overlaps axes label when using twiny
问题描述
我正在尝试使用 twiny 在同一张图上绘制两个独立的数量,如下所示:
I am trying to plot two separate quantities on the same graph using twiny as follows:
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 轴上的轴标签重叠,因此几乎难以辨认(我想在此处发布图片示例,但我没有)还没有足够高的代表).
and the data is presented fine, but I am having the problem that the figure title is overlapping with the axes labels on the secondary x axis so that it's barely legible (I wanted to post a picture example here, but I don't have a high enough rep yet).
我想知道是否有一种简单的方法可以将标题直接向上移动几十个像素,以便图表看起来更漂亮.
I'd like to know if there's a straightforward way to just shift the title directly up a few tens of pixels, so that the chart looks prettier.
推荐答案
我不确定它是否是 matplotlib 更高版本中的新功能,但至少对于 1.3.1,这只是:
I'm not sure whether it is a new feature in later versions of matplotlib, but at least for 1.3.1, this is simply:
plt.title(figure_title, y=1.08)
这也适用于 plt.suptitle()
,但不适用于 plt.xlabel()
等.
This also works for plt.suptitle()
, but not (yet) for plt.xlabel()
, etc.
这篇关于使用 twiny 时,Python Matplotlib 图形标题与轴标签重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!