繁体   English   中英

如何将标题放在matplotlib中图形的底部?

[英]How to put the title at the bottom of a figure in matplotlib?

我使用 matplotlib 绘制了一个包含四个子图的图形, set_title方法将标题( (a) (b) (c) (d) )放在每个子图的顶部,请参阅以下代码示例。

fig = pyplot.figure()
ax = fig.add_subplot(1, 4, 1)
ax.set_title('(a)')

但我想把每个标题放在每个子图的底部。 我无法通过 matplotlib 文档和谷歌弄清楚它。 所以我需要你的帮助,非常感谢。

在此处输入图片说明

由于您没有使用 x 轴,您可以简单地将 xlabel 设置为标题,应该注意定位:

ax.set_xlabel('this really is a title disguised as an x label')

编辑:

尝试根据图形高度偏移标题,我希望这有效:

size = fig.get_size_inches()*fig.dpi # get fig size in pixels
ax.set_title('(a)', y=-size) # increase or decrease y as needed

这是一个小的python函数,可以绘制没有轴的图像。 每个子图像底部的标题。 images是一个长度为 n 的数组,其中包含内存中的图像, labels是一个长度为 n 的数组,带有相应的标题:

from matplotlib import pyplot

def plot_image_array_gray(images, labels):
  for i in range(0, len(labels)):
    ax = pyplot.subplot(1, len(labels), i + 1)
    pyplot.axis('off')
    pyplot.text(0.5, -0.1, labels[i], \
      horizontalalignment='center', verticalalignment='center', \
      transform=ax.transAxes)
    pyplot.imshow(images[i], cmap=pyplot.cm.gray)

  pyplot.tight_layout()
  pyplot.show()

使用示例

# code to load image_a and image_b 
# ...
plot_image_array_gray((image_a, image_b), ("(a)", "(b)"))

如果您使用 ax,请使用以下行,然后调整 'y' 的值。

ax.set_title('(d)',y=-0.2,pad=-14)

您可以调整 y 值,这里我使用“负”值,因为我希望我的标签位于子图中的图形底部。

我还没有检查什么是垫。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM