2

enter image description hereI am creating a figure with subplots using:

fig, axs = plt.subplots(5, sharex=True, sharey=True, gridspec_kw={'hspace': 0})

Then I want to add a title to each subplot, but with the title inside the subplot. I tried using

for i in range(0,5):
    axs[i].set_title('title = ' + str(i), pad = -15)

But this approach creates the last title again in the subplot above, for reasons I don't yet understand. I also tried appending an additional subplot, but couldn't find a convenient way to get rid of it again.

1
  • There was a bug in matplotlib that is fixed in the 3.3 release which is upcoming. If you can use the prerelease please try that and see if this is fixed? If not, feel free to open a bug report at github. Commented Jul 2, 2020 at 15:45

1 Answer 1

3

I don't know why this happens using pad. However, a workaround might be to use the argument y= to specify a position for the title text, along with changing the vertical alignment of the text:

fig, axs = plt.subplots(5, sharex=True, sharey=True, gridspec_kw={'hspace': 0})

for i in range(0,5):
    axs[i].set_title('title = ' + str(i), y=0.8, va="top")

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This is pretty convenient and does what I was looking for. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.