Skip to content

Vertical alignment of text is too high #2527

Closed

Description

@breedlun

One of the improvements to matplotlib 1.3.0 is supposed to be improved vertical alignment of text, but it appears to have gotten worse in my experience. Here is an example:

import matplotlib.pylab as plt
mpl.rcParams['font.size'] = 24
fig = plt.figure(figsize = (5,5))
ax = plt.subplot(111)
ax.plot([0,1], [0,1])

txts = ['1', '2', '3']
y_coords = [0.8, 0.5, 0.2]
for txt, y_coord in zip(txts, y_coords):
    ax.annotate(txt, xy = (0.25, y_coord), fontname = 'Helvetica', \
        size=36, va='center', ha='center', \
        bbox=dict(boxstyle='square', fc = 'w'))

plt.savefig('Test.png', bbox_inches = 'tight', pad_inches = 0)

In matplotlib 1.2.1 the png looks like this

test_mpl_1 2 1

but in matplotlib 1.3.0 the png looks like this

test_mpl_1 3

The numbers in the the v1.3.0 png are too high. The numbers should be in the center of the rectangular boxes, and the center of the tick mark numbers should line up with the tick marks.

Activity

  1. pelson commented on Oct 18, 2013

    @pelson
    Member

    Pinging @leejjoon just in case you have any ideas.

  2. WeatherGod commented on Oct 18, 2013

    @WeatherGod
    Member

    I am wondering if this is related to changes in how the text's baseline is
    treated. Also, wasn't there differences with how Agg handled it and PDF and
    other backends handled it?

  3. leejjoon commented on Oct 20, 2013

    @leejjoon
    Contributor

    My guess is that texts seem to high because of the extra padding below the texts. One of the change made by @mdboom is that a minimum descent (the descent of "g") is accounted when the bounding box is calculated. The advantage of this is that the bounding boxes of different texts look consistent (e.g., bounding boxes of "a" and "g" look similar). But some may feel that there are too much space. I am not sure if there is an option to change the default behavior.

  4. breedlun commented on Oct 20, 2013

    @breedlun
    ContributorAuthor

    Yeah, in my humble opinion, "g" should have a different bounding box than "a", but they should have the same baseline. Perhaps there is a reason to make all characters have the same bounding box, but, in my view, the bounding box reflects that different characters are different dimensions. When I want to treat different characters as if they were the same size, then I just use some multiple of the font size.

    For example, I can center the text inside the squares (see my original post) by aligning the squares to the text bounding box. Tick mark labels, on the other hand, can be vertically aligned according to the baseline and half the font size.

    Well, if you put in the option to switch how the bounding box is calculated, either behavior as a default is fine with me. The real lure of matplotlib for me is it's customizability.

  5. breedlun commented on Jan 16, 2014

    @breedlun
    ContributorAuthor

    @mdboom made a reference to this issue on issue 2519. He said, "In 1.3.0, the bounding box now includes the descenders. This is a bugfix for a number of layout problems in the legend and elsewhere, and I consider the new behavior correct." This echoes the comment above by @leejjoon .

    I am kind of surprised to hear that this is considered correct behavior. Take a look at the tick mark labels for the vertical axis in the v1.2.1 plot above. The vertical center of the numbers are nicely aligned with the tick marks. In the v1.3.0 plot above, the numbers are all too high relative to their respective tick marks.

    Perhaps there is some kind of middle ground that continues to fix the layout problems that @mdboom refers to, while also fixing the issues I've pointed out.

  6. tacaswell commented on Feb 21, 2014

    @tacaswell
    Member

    @mdboom Can you take another look it this?

  7. modified the milestones: v1.4.0, v1.3.x, v1.4.x on Apr 17, 2014
  8. modified the milestones: , v1.4.x on Feb 7, 2015
  9. modified the milestones: , on Jul 17, 2015
  10. modified the milestones: , on Feb 15, 2016
  11. efiring commented on Feb 15, 2016

    @efiring
    Member

    @mdboom, is there some possible compromise? Maybe reduce the bottom padding? Aesthetically, @breedlun is right; for numbers or anything without descenders, the present centering doesn't look good.

  12. added a commit that references this issue on Mar 21, 2016
    385d76e
  13. self-assigned this
    on Mar 21, 2016
  14. added a commit that references this issue on Apr 11, 2016
    f8a1802
  15. breedlun commented on Apr 15, 2016

    @breedlun
    ContributorAuthor

    Does #6200 fix the vertical alignment for all text, or just y-tick mark text?

  16. tacaswell commented on Apr 15, 2016

    @tacaswell
    Member

    Just y tick by default, but introduced a new alignment value you can use
    anywhere.

    On Thu, Apr 14, 2016, 20:45 Benjamin Reedlunn [email protected]
    wrote:

    Does #6200 #6200 fix the
    vertical alignment for all text, or just y-tick mark text?

    —
    You are receiving this because you commented.
    Reply to this email directly or view it on GitHub
    #2527 (comment)

  17. breedlun commented on Apr 15, 2016

    @breedlun
    ContributorAuthor

    OK. I will try out this new alignment value before v2.0 gets released.

  18. breedlun commented on Feb 5, 2017

    @breedlun
    ContributorAuthor

    I finally got around to trying v2.0. The alignment of vertical tick mark labels looks great! In addition, I noticed that the subscripts and superscripts generated by mathtext have better vertical positioning too. Thanks @mdboom! Virtually none of the default changes in v2.0 affect me (I have all my own defaults set up), but the improved vertical alignment/positioning made the upgrade worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions