14

I use matplotlib to plot a figure with four subfigures, and set_title method put the title ( (a) (b) (c) (d)) on the top of every subfigure, see the following code example.

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

But I want to put every title at the bottom of every subfigure. I can't figure out it by the matplotlib document and google. So I need your help, thanks very much.

enter image description here

Huayi Wei
  • 829
  • 1
  • 7
  • 16
  • would this popular question please be answered? one can shift it loc=left/center/right but not vertically and y=-1 is a mess – droid192 Mar 04 '19 at 12:52

3 Answers3

5

Since you're not using the x axis you can just simply set the xlabel to act as the title, should take care of the positioning:

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

Edit:

Try offsetting the title according to the figure height, I hope this works:

size = fig.get_size_inches()*fig.dpi # get fig size in pixels
ax.set_title('(a)', y=-size) # increase or decrease y as needed
ifma
  • 3,673
  • 4
  • 26
  • 38
  • 1
    This can't work for my case, because I need to set axis off, so the xlabel don't show up. – Huayi Wei Jul 04 '16 at 13:12
  • Why does it need to be off? You can set it on so that the xlabel is the title, which was my first suggestion. You can also try the 2nd one, I edited my answer I hope it helps – ifma Jul 04 '16 at 23:42
  • 1
    Your answer using `set_title` doesn't work if one is using subplots. It also needs to be formulated `y=-size[1]` as `size` is a two-element list. – Björn Lindqvist Mar 21 '19 at 14:51
3

Here is a small python function that plot images without axis. Titles at the bottom of each sub-image. images is a n-length array with the images in memory and labels is a n-length array with the corresponding titles:

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()

Example of usage

# code to load image_a and image_b 
# ...
plot_image_array_gray((image_a, image_b), ("(a)", "(b)"))
Lucho
  • 311
  • 2
  • 4
3

Please use the following line if you are using ax, then adjust the value of 'y'.

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

You can adjust the y value, here I use 'negative' value because I want my label to be on the bottom of the graph within subplot.

I have not check what is pad yet.

Òscar Raya
  • 598
  • 5
  • 26