Skip to content

Weird behaviour of suptitle() when horizontalalignment is not 'center' #12197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gansanay opened this issue Sep 21, 2018 · 8 comments
Closed
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones!
Milestone

Comments

@gansanay
Copy link

gansanay commented Sep 21, 2018

Bug report

Bug summary

horizontalalignment = 'left' aligns the title on the right, horizontalalignment = 'right' aligns a short title on the left, horizontalalignment = 'right' displaces a longer title on the far left

Code for reproduction

# Left alignment test, short title
fig = plt.figure(figsize=(9,4))
ax = fig.add_subplot(111)
fig.suptitle('This figure suptitle should be on the left', horizontalalignment = 'left')
ax.set_title('This axis title should be on the left', loc='left');
fig.savefig('left_align.png')

# Right alignment test, short title
fig = plt.figure(figsize=(9,4))
ax = fig.add_subplot(111)
fig.suptitle('This figure suptitle should be on the right', horizontalalignment = 'right')
ax.set_title('This axis title should be on the right', loc='right');
fig.savefig('right_align.png')

# Right alignment test, longer title
fig = plt.figure(figsize=(9,4))
ax = fig.add_subplot(111)
fig.suptitle('This figure suptitle should be on the right but it is far left', horizontalalignment = 'right')
ax.set_title('This axis title should be on the left', loc='left')
fig.savefig('far_left.png');

Actual outcome

  • Left alignment test, short title
    left_align

  • Right alignment test, short title
    right_align

  • Right alignment test, longer title
    far_left

Expected outcome

The following code manually sets the figure title at the expected position:

fig = plt.figure(figsize=(9,4))
ax = fig.add_subplot(111)
fig.suptitle('This figure suptitle is long but it is manually positioned', x = 0.38)
ax.set_title('This axis title should be on the left', loc='left')
fig.savefig('manual_pos.png');

Which gives:
manual_pos

Matplotlib version

  • Operating system: Windows
  • Matplotlib version: 2.1.2 and 2.2.3
  • Matplotlib backend: agg
  • Python version: 3.6.4
  • Jupyter version: 4.4.0
@ImportanceOfBeingErnest
Copy link
Member

This is all expected. The suptitle is in all cases positionned in the horizontal center of the figure. horizontalalignment aligns the text relative to this center position, i.e. if for horizontalalignment=left, the left end of the text is at the center position, for horizontalalignment=right the right end of the text is at the center.

image

The suptitle does not have a loc parameter. In order to position a suptitle you may however use the x and y arguments. The position is in figure coordinates. So if you want to have a suptitle aligned with the (only) subplot in a figure you may set its position according to the subplot paramters, e.g. the left parameter (fig.subplotpars.left) is 0.125. You will still need to set the aligment as well.

fig = plt.figure(figsize=(9,4))
ax = fig.add_subplot(111)
fig.suptitle('This figure suptitle should be on the left', 
             horizontalalignment = 'left', x=fig.subplotpars.left) # x=0.125
ax.set_title('This axis title should be on the left', loc='left');

# Right alignment test, short title
fig = plt.figure(figsize=(9,4))
ax = fig.add_subplot(111)
fig.suptitle('This figure suptitle should be on the right', 
             horizontalalignment = 'right', x=fig.subplotpars.right) # x=0.9
ax.set_title('This axis title should be on the right', loc='right');

# Right alignment test, longer title
fig = plt.figure(figsize=(9,4))
ax = fig.add_subplot(111)
fig.suptitle('This figure suptitle should be on the right and it actually is.', 
             horizontalalignment = 'right', x=fig.subplotpars.right) # x=0.9
ax.set_title('This axis title should be on the left', loc='left')

plt.show()

image

I will close this because I don't think there is anything wrong with the library here. Feel free to reopen in case you disagree.

@gansanay
Copy link
Author

Thank you for your answer!

Do you agree this should be documented?

@ImportanceOfBeingErnest
Copy link
Member

subtitle documents

x : float, default 0.5 The x location of the text in figure coordinates.
y : float, default 0.98 The y location of the text in figure coordinates.
**kwargs: Additional kwargs are matplotlib.text.Text properties.

matplotlib.text.Text documents

horizontalalignment: {'center', 'right', 'left'}

There is the Text alignment example which shows the different alignments.

At which point exactly do you think the documentation should be extended? We're always open to improvements of the docs, so feel free to make a suggestion here.

@timhoffm
Copy link
Member

suptitle could be more precise in documenting horizontalalignment and verticalalignment e.g.

The horizontal alignment of the text relative to (*x*, *y*).

Feel free to open a PR.

@ImportanceOfBeingErnest
Copy link
Member

Since horizontalalignment isn't really an argument of suptitle but part of the kwargs, which are passed on to Text, do you feel one should make an exception here and document this kwarg explicitely?

I would have though maybe documenting Text's horizontalalignment in a bit more than half a sentence could make sense?

(reopening this to let the discussion evolve)

@timhoffm
Copy link
Member

It's already documented separately, because the defaults are different from Text.
https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.suptitle

@jklymak jklymak added the Good first issue Open a pull request against these issues if there are no active ones! label Sep 21, 2018
@ImportanceOfBeingErnest
Copy link
Member

True, I must have looked at a completely different page, despite linking the correct one. 😳

@gansanay Is the above proposal by @timhoffm

The horizontal alignment of the text relative to (x, y).

how you imagine this to be clearified? Would you like to change it yourself?

@thoo
Copy link
Contributor

thoo commented Oct 18, 2018

Should I also need to generate any file? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones!
Projects
None yet
Development

No branches or pull requests

5 participants