5

Using matplotlib 1.4.3 and the following code, the figure and suptitle displays correctly, however upon saving, the suptitle is removed.

true_vals = [1,2,3]

f, ax_arr = plt.subplots(1,3,figsize=(15,5))
ax_arr = ax_arr.reshape(-1)
f.suptitle("This is my suptitle\nThis is the second line", fontsize=20, y=1.1) 
# y is set to 1.1 to keep the second line in the suptitle from hitting the top of the subplots.

for idx, i in enumerate(true_vals):
    ax_arr[idx].boxplot(data[:,idx], labels=i)

f.savefig('suptitle_test.pdf', dpi=f.dpi)

1 Answer 1

11

Using the advice given here,

adding the following to the savefig command will produce a tight plot, keeping the suptitle in the saved figure:

true_vals = [1,2,3]

f, ax_arr = plt.subplots(1,3,figsize=(15,5))
ax_arr = ax_arr.reshape(-1)
my_suptitle = f.suptitle("This is my suptitle\nThis is the second line", fontsize=20, y=1.1) 
# y is set to 1.1 to keep the second line in the suptitle from hitting the top of the subplots.

for idx, i in enumerate(true_vals):
    ax_arr[idx].boxplot(data[:,idx], labels=i)

f.savefig('suptitle_test.pdf', dpi=f.dpi, bbox_inches='tight',bbox_extra_artists=[my_suptitle])
Sign up to request clarification or add additional context in comments.

1 Comment

Just to stress the bbox_inches='tight' argument must be used otherwise the figure will be rendered without suptitle. See explanation in the matplotlib reference matplotlib.org/stable/api/_as_gen/…

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.