使用 twiny 时,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 轴上的轴标签重叠,因此几乎无法辨认(我想在这里发布图片示例,但我没有足够高的代表)。

我想知道是否有一种简单的方法可以将标题直接向上移动几十个像素,从而使图表看起来更漂亮。

原文由 Magic_Matt_Man 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 369
2 个回答

我不确定它是否是 matplotlib 更高版本中的新功能,但至少对于 1.3.1,这很简单:

 plt.title(figure_title, y=1.08)

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

原文由 herrlich10 发布,翻译遵循 CC BY-SA 3.0 许可协议

忘记使用 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()

在此处输入图像描述

原文由 Hooked 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题