Skip to content

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

Closed

Description

@gansanay

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

Activity

  1. ImportanceOfBeingErnest commented on Sep 21, 2018

    @ImportanceOfBeingErnest
    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.

  2. gansanay commented on Sep 21, 2018

    @gansanay
    Author

    Thank you for your answer!

    Do you agree this should be documented?

  3. ImportanceOfBeingErnest commented on Sep 21, 2018

    @ImportanceOfBeingErnest
    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.

  4. timhoffm commented on Sep 21, 2018

    @timhoffm
    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.

  5. ImportanceOfBeingErnest commented on Sep 21, 2018

    @ImportanceOfBeingErnest
    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)

  6. timhoffm commented on Sep 21, 2018

    @timhoffm
    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

  7. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Sep 21, 2018
  8. ImportanceOfBeingErnest commented on Sep 22, 2018

    @ImportanceOfBeingErnest
    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?

  9. thoo commented on Oct 18, 2018

    @thoo
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Documentation🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions