Python Matplotlib figure title overlaps axes label when using twiny
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Question
00:59 Accepted answer (Score 296)
01:23 Answer 2 (Score 38)
02:08 Answer 3 (Score 20)
02:44 Answer 4 (Score 12)
02:59 Thank you
--
Full question
https://stackoverflow.com/questions/1275...
Answer 2 links:
[image]: https://i.stack.imgur.com/mZusF.png
[image]: https://i.stack.imgur.com/Re5SH.png
https://matplotlib.org/users/tight_layou...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib #title #figure
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Question
00:59 Accepted answer (Score 296)
01:23 Answer 2 (Score 38)
02:08 Answer 3 (Score 20)
02:44 Answer 4 (Score 12)
02:59 Thank you
--
Full question
https://stackoverflow.com/questions/1275...
Answer 2 links:
[image]: https://i.stack.imgur.com/mZusF.png
[image]: https://i.stack.imgur.com/Re5SH.png
https://matplotlib.org/users/tight_layou...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib #title #figure
#avk47
ACCEPTED ANSWER
Score 299
I'm not sure whether it is a new feature in later versions of matplotlib, but at least for 1.3.1, this is simply:
plt.title(figure_title, y=1.08)
This also works for plt.suptitle(), but not (yet) for plt.xlabel(), etc.
ANSWER 2
Score 37
Forget using plt.title and place the text directly with plt.text. An over-exaggerated example is given below:
import pylab as plt
fig = plt.figure(figsize=(5,10))
figure_title = "Normal title"
ax1 = plt.subplot(1,2,1)
plt.title(figure_title, fontsize = 20)
plt.plot([1,2,3],[1,4,9])
figure_title = "Raised title"
ax2 = plt.subplot(1,2,2)
plt.text(0.5, 1.08, figure_title,
horizontalalignment='center',
fontsize=20,
transform = ax2.transAxes)
plt.plot([1,2,3],[1,4,9])
plt.show()

ANSWER 3
Score 25
I was having an issue with the x-label overlapping a subplot title; this worked for me:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 1)
ax[0].scatter(...)
ax[1].scatter(...)
plt.tight_layout()
.
.
.
plt.show()
before
after
reference:
ANSWER 4
Score 12
ax.set_title('My Title\n', fontsize="15", color="red")
plt.imshow(myfile, origin="upper")
If you put '\n' right after your title string, the plot is drawn just below the title. That might be a fast solution too.

