How to make a title for this multi-axis matplotlib plot?

Question:

This function:

def plotGrid(ax, grid, text=''):
  ax.imshow(grid, cmap=cmap, norm=Normalize(vmin=0, vmax=9))
  ax.grid(True, which='both', color='lightgrey', linewidth=0.5)  
  ax.set_yticks([x-0.5 for x in range(1+len(grid))])
  ax.set_xticks([x-0.5 for x in range(1+len(grid[0]))])   
  ax.set_xticklabels([])
  ax.set_yticklabels([])
  ax.set_title(text)
  

def plotTaskGrids(task):
  nTrain = len(task['train'])
  fig, ax = plt.subplots(2, nTrain, figsize=(3*nTrain, 3*2))

  for i in range(nTrain):   
    plotGrid(ax[0, i], task['train'][i]['input'], 'train input')
    plotGrid(ax[1, i], task['train'][i]['output'], 'train output')

  plt.tight_layout()
  plt.title('title')
  plt.show()    

displays this window:

enter image description here

I would like to replace Figure 1 in the window title with title, but plt.title('title') doesn’t accomplish that, instead it changes one of the subtitles. What is the solution?

Asked By: Paul Jurczak

||

Answers:

Perhaps you could try adding num="title" when calling plt.subplots?

fig, ax = plt.subplots(2, nTrain, figsize=(3*nTrain, 3*2), num="title")

This should get passed to the plt.figure call within the subplots call.

Answered By: Ineruditus
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.