1

In Matplotlib, centering the title centers it relative to the main body of the chart.

Instead, I want to center it relative to the entire plot, including the y axis and the y axis label.

How can I do this? I'm assuming that at the very least I can manually define the exact horizontal position of the title, but I can't even figure out how to do that.

This generates a plot using title (in red) and supertitle (in blue). They're in slightly different positions, but they both appear to be centered relative to the main chart area of the text.

import pandas as pd
import matplotlib.pyplot as plt
month = ['Jan', 'Feb', 'Mar', 'Apr']
volume = [2900, 1500, 2400, 1200]
d = pd.DataFrame({'Month': month, 'Volume': volume})
_, ax = plt.subplots()
ax.set(ylabel='Volume (cbm)', xlabel='')
b = d.plot(kind='bar', y="Volume", x="Month", ax=ax)
plt.title('Title', color='red')
plt.suptitle('Title', color='blue')
plt.show()

The output:

the output

AnonymousCowherd
  • 145
  • 2
  • 11
  • 1
    If you have a single subplot, use `plt.suptitle()` as shown in [this question](https://stackoverflow.com/questions/7066121/how-to-set-a-single-main-title-above-all-the-subplots-with-pyplot). I would hence mark this as duplicate. – ImportanceOfBeingErnest Sep 28 '17 at 07:44
  • Thanks @ImportanceOfBeingErnest. I've edited the question to show the result of using plt.suptitle(). It does move slightly, but still appears to be centered above the main body of the chart. – AnonymousCowherd Sep 28 '17 at 16:29
  • 1
    `plt.suptitle` puts the title in the middle of the figure. [Example image](https://i.stack.imgur.com/W1vjN.png). Since you haven't shared all information to reproduce the issue, one cannot help here. If you have a problem, you may of course show a [mcve] of the issue which would allow to actually help you solve it. – ImportanceOfBeingErnest Sep 29 '17 at 09:32
  • Yes, that sloppy of me. I've rewritten the question with a code example. I can put images back in, but I'm not sure they help. Your example looks like it is doing what I want. Where is this example from? A reverse image search doesn't turn anything up, so I'm assuming you created it. – AnonymousCowherd Sep 29 '17 at 16:28
  • Yes, I created that image. But also your code does what you are asking. The blue title is **exctly** in the middle of the figure (letters start 203 pixels from the left and end 203 pixels from the right), and that is what the question asks for, right? – ImportanceOfBeingErnest Sep 29 '17 at 19:42
  • You're right. Not sure what I was thinking. Should I delete this non-question? – AnonymousCowherd Oct 03 '17 at 20:37
  • Since it is closed anyways and not really clear, you may indeed just delete it. – ImportanceOfBeingErnest Oct 03 '17 at 20:42

0 Answers0