如何为每个子图添加标题

新手上路,请多包涵

我有一个包含许多子图的图。

 fig = plt.figure(num=None, figsize=(26, 12), dpi=80, facecolor='w', edgecolor='k')
fig.canvas.set_window_title('Window Title')

# Returns the Axes instance
ax = fig.add_subplot(311)
ax2 = fig.add_subplot(312)
ax3 = fig.add_subplot(313)

如何为子图添加标题?

fig.suptitle 为所有图表添加标题,虽然 ax.set_title() 存在,但后者不会为我的子图添加任何标题。

谢谢您的帮助。

编辑:更正了关于 set_title() 的错字。谢谢罗格·卡西斯

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

阅读 2k
2 个回答

ax.title.set_text('My Plot Title') 似乎也可以。

 fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.title.set_text('First Plot')
ax2.title.set_text('Second Plot')
ax3.title.set_text('Third Plot')
ax4.title.set_text('Fourth Plot')
plt.show()

matplotlib 在子图上添加标题

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

ax.set_title() 应该为单独的子图设置标题:

 import matplotlib.pyplot as plt

if __name__ == "__main__":
    data = [1, 2, 3, 4, 5]

    fig = plt.figure()
    fig.suptitle("Title for whole figure", fontsize=16)
    ax = plt.subplot("211")
    ax.set_title("Title for first plot")
    ax.plot(data)

    ax = plt.subplot("212")
    ax.set_title("Title for second plot")
    ax.plot(data)

    plt.show()

你能检查一下这段代码是否适合你吗?也许以后有什么东西会覆盖它们?

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

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