Skip to content

Saving as a pdf shifts the bounding box to the right for text with subscripts #2627

Closed

Description

@breedlun

The following 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 = ['$\\theta$', '$\\theta_1$', '$\\alpha_2$']
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)
plt.savefig('Test.pdf', bbox_inches = 'tight', pad_inches = 0)

produces two files. Here is Test.png:

test

In the png, the vertical alignment of the bounding box is too low for the \theta in the top box, as I previously reported in Issue #2527, but otherwise it is fine. Here is Test.pdf:

test

In the pdf, the horizontal alignment of the bounding box for \theta_1 and \alpha_2 have been shifted to the right. In addition, the right side of the 0 in the lower right of the pdf has been cut off in the pdf, but it was not cut off in the png.

In case it matters, I generated these using Matplotlib 1.3.0 with the TkAgg backend.

Activity

  1. added this to the v1.4.x milestone on Jul 18, 2014
  2. modified the milestones: , v1.4.x on Feb 7, 2015
  3. modified the milestones: , on Jul 17, 2015
  4. efiring commented on May 22, 2016

    @efiring
    Member

    With master and classic style there are still subtle (but smaller) differences in positioning between pdf and png, but the 0 is not being cut off.

  5. breedlun commented on May 23, 2016

    @breedlun
    ContributorAuthor

    Ok, good to hear the problem has been reduced.

  6. modified the milestones: , on Oct 3, 2017
  7. QuLogic commented on Apr 22, 2020

    @QuLogic
    Member

    The original PDF screenshot is no longer accessible. When I run the example, there is a slight shift, but that happens for all the text between the two versions. The bounding boxes seem right, but I don't know what the original problem was.

  8. breedlun commented on Apr 24, 2020

    @breedlun
    ContributorAuthor

    I tried to run the example in an old version of matplotlib version I had on my hard drive (1.5.dev1) to see if I could recreate the original issue, but pyplot failed to import. With matplotib 2.2.3, I can confirm that subtle differences exist between the png and pdf, but the bounding boxes seem reasonably centered in both. Either I am less picky 6.5 years later, or something got fixed between now and then. Thanks for looking into this.

  9. 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