Skip to content

annotate places the number 1 off center when saved as a pdf #2519

Closed

Description

@breedlun

I am finding that the annotate function places the number 1 off center when the figure is saved as a pdf. All other numbers appear to be fine. For example,

import matplotlib.pylab as plt
fig = plt.figure(figsize = (5,5))
ax = plt.subplot(111)
ax.plot([0,1], [0,1])

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

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

produces a pdf file that looks like this

pdf

and a jpg file that looks like this

jpg

In the pdf, the 1 is not in the center of the rectangle, but the 2 and 3 are in the centers of their respective rectangles. In the jpg, all three numbers are in the center of their respective rectangles. If I was creating rasterized images I would be happy with the jpg, but I really want a pdf output.

Is this a bug?

I am using Matplotlib 1.2.1 on Python 2.7.3 under Mac OS X 10.8.5

Activity

  1. pelson commented on Jan 9, 2014

    @pelson
    Member

    This looks like it has been fixed in v1.3.0 and above. Are you able to test with a later version of matplotlib?

    Thanks,

  2. breedlun commented on Jan 9, 2014

    @breedlun
    ContributorAuthor

    I just tried it in v1.3.0, and the issue is not fixed in my setup. In case it matters, I am running Python 2.7.5, via Spyder 2.2.5, under Mac OS X 10.8.5.

    Here is the output for the pdf

    pdf

    The 1 is still off center in the pdf.

    Here is the output for the jpg

    jpg

    The 1 is horizontally centered in the jpg.

    (It probably is not important, but I had to change y_coord in my original post from [0.9, 0.8, .07] to [0.9, 0.7, 0.5]. I think the original [0.9, 0.8, .07] was a typo.)

    Also, now that I have switched to Matplotlib 1.3.0, you can see that the jpg and pdf both have the problem I reported in issue 2527. The vertical alignment of the text inside the bounding box is too high.

  3. reopened this on Jan 9, 2014
  4. pelson commented on Jan 10, 2014

    @pelson
    Member

    Thanks @stretch97 - Sorry about the premature closing. @mdboom has re-opened.

    Cheers!

  5. mdboom commented on Jan 13, 2014

    @mdboom
    Member

    As for vertical alignment: 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.

    As for the horizontal alignment, I'm not able to reproduce, but I think that's mainly because I don't have the Helvetica font installed. Where did you get it? (I can't just Google it -- there's so many incompatible versions floating around, I need to ensure I'm using the same one you are in order to reproduce).

  6. breedlun commented on Jan 14, 2014

    @breedlun
    ContributorAuthor

    I will respond to your comment about the vertical alignment in issue 2527.

    The Helvetica font was already installed on my system (Mac OS X 10.8), so I just stuck a copy in the mpl-data/fonts/ttf/ directory. (If you want a free Helvetica clone you could try URW Nimbus Sans, or the TeX Gyre Heros fonts.)

    I am not sure the issue is isolated to the Helvetica font, though. Here is the updated version using the default Bitstream Vera Sans.

    import matplotlib.pylab as plt
    fig = plt.figure(figsize = (5,5))
    ax = plt.subplot(111)
    ax.plot([0,1], [0,1])
    
    txts = ['1', '2', '3']
    y_coords = [0.9, 0.7, 0.5]
    for txt, y_coord in zip(txts, y_coords):
        ax.annotate(txt, xy = (0.25, y_coord), \
            size=36, va='center', ha='center', fontname = 'BitStream Vera Sans', \
            bbox=dict(boxstyle='square', fc = 'w'))
    
    plt.savefig('Test.pdf', bbox_inches = 'tight', pad_inches = 0)
    plt.savefig('Test.jpg', bbox_inches = 'tight', pad_inches = 0)
    

    First the pdf,

    pdf

    and then the jpg,

    jpg

    The numbers in the pdf still look like they are too far to the right, while the numbers in the jpg look fine.

  7. breedlun commented on Jul 24, 2014

    @breedlun
    ContributorAuthor

    I am not sure, but this issue may be related to issue #253.

  8. added this to the v1.4.x milestone on Aug 17, 2014
  9. modified the milestones: , v1.4.x on Feb 7, 2015
  10. modified the milestones: , on Jul 17, 2015
  11. story645 commented on Jun 1, 2016

    @story645
    Member

    this seems really similar to #2627

  12. modified the milestones: , on Oct 3, 2017
  13. QuLogic commented on Apr 22, 2020

    @QuLogic
    Member

    This looks perfectly fine to me, but the original screenshots are gone, so I don't know what the problem was.

  14. breedlun commented on Apr 24, 2020

    @breedlun
    ContributorAuthor

    When I run

    import matplotlib.pylab as plt
    fig = plt.figure(figsize = (5,5))
    ax = plt.subplot(111)
    ax.plot([0,1], [0,1])
    
    txts = ['1', '2', '3']
    y_coords = [0.9, 0.7, 0.5]
    for txt, y_coord in zip(txts, y_coords):
        ax.annotate(txt, xy = (0.25, y_coord), \
            size=36, va='center', ha='center', fontname = 'BitStream Vera Sans', \
            bbox=dict(boxstyle='square', fc = 'w'))
    
    plt.savefig('Test.pdf', bbox_inches = 'tight', pad_inches = 0)
    plt.savefig('Test.png', bbox_inches = 'tight', pad_inches = 0)
    

    in matplotlib 2.2.3, here is the pdf:
    Test.pdf
    and here is the png:
    Test

    They look pretty similar to me. Either something changed over the last 6.5 years or I became less picky. (I tried to load an old version of matplotlib on my hard drive to see if things were different 6.5 years ago, but pyplot failed to import.) My only complaint is the bounding boxes are too low relative to the text in both the pdf and png, but that is different from the original issue, so I am closing.

  15. removed this from the future releases milestone on Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions