Skip to content

savefig PDF output - incorrect annotation location #10844

Closed

Description

@delorytheape

Bug report

Annotation beneath legend is positioned correctly on screen and with PNG output generated by savefig(). Annotation location is incorrect for PDF output of savefig()

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.text

fig, ax = plt.subplots()

ax.plot([5,1], label="Label 1")
ax.plot([3,0], label="Label 2")

legend = ax.legend(loc="upper right")

# Create offset from lower right corner of legend box,
# (1.0,0) is the coordinates of the offset point in the legend coordinate system
offset = matplotlib.text.OffsetFrom(legend, (1.0, 0.0))
# Create annotation. Top right corner located -20 pixels below the offset point 
# (lower right corner of legend).
ax.annotate("info_string", xy=(0,0),size=14,
            xycoords='figure fraction', xytext=(0,-20), textcoords=offset, 
            horizontalalignment='right', verticalalignment='top')
# Draw the canvas for offset to take effect
fig.canvas.draw()

fig.savefig('plot1.png')
fig.savefig('plot1.pdf')

plt.show()

Actual outcome

The code produces an on-screen plot with two lines, a legend, and the annotation string 'info_string' located beneath the legend and aligned to the right hand edge of the legend box.
The code also generates two files;

  1. 'plot1.png', which is generated correctly
  2. 'plot1.pdf', which is generated incorrectly. The annotation is not visible, as it is in the wrong location (far right of the displayed region).

The PNG output is here, which is what both files should look like.
The incorrect PDF output is here.

If the length of the annotation string in increased (eg. to 'extra_information_string'), the first few characters become visible in the top right corner of the PDF file.

Expected outcome
The PDF file should show the annotation in the same location as the on screen plot and the PNG file.

Software Versions

  • Operating system: Windows 7 Home
  • Matplotlib version: 2.2.0 (problem also appears with 1.5.3)
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 2.7.14
  • Jupyter version (if applicable): N/A
    Matplotlib and python were installed using conda from the default channel.
    The problem was observed when running the above code as a script from the command line.

Activity

  1. delorytheape commented on Mar 20, 2018

    @delorytheape
    Author

    Thanks for cross referencing this for me IOBE. I have not submitted a bug report before...

  2. QuLogic commented on Mar 20, 2018

    @QuLogic
    Member

    This is because the offset you get is with the default DPI, but PDF always uses 72. You can workaround it by explicitly setting the DPI with plt.subplots(dpi=72), though you end up with a slightly smaller PNG and interactive plot.

    The correct fix is to somehow get OffsetFrom and/or annotate to use the right renderer, but I'm not sure how difficult that is.

  3. afvincent commented on Mar 20, 2018

    @afvincent
    Contributor

    Well, 👍 that something is wrong with how DPI value is handled. Even the PNG outputs can become weird, see for example (with the snippet from above):

    for dpi in (100, 200, 300):
        fig.savefig('plot1_{}.png'.format(dpi), dpi=dpi)
    • 100 dpi-PNG (default, is it not?) plot1_100
    • 200 dpi-PNG plot1_200
    • 300 dpi-PNG plot1_300
  4. efiring commented on Mar 20, 2018

    @efiring
    Member

    Based on a quick look at the code, I don't see where the problem is coming from. It looks like everything is being updated with every draw, using the renderer in effect at draw-time.

  5. added this to the milestone on Mar 21, 2018
  6. jklymak commented on Mar 28, 2018

    @jklymak
    Member

    Its definitely a dpi issue. If I change the x,y by renderer.dpi / 200 I get the right positioning. However, I have no way to programatically get the 200 from within text.OffsetFrom. self._artist.figure.dpi returns the same as renderer.dpi at this point. But when it calls self._artist.get_window_extent(renderer), it gets co-oridnates at 200 dpi. (It ignores the renderer argument, as do most artists for whatever reason). I can't see a good way of guessing the original figure dpi from within text.OffsetFrom, but its late so maybe I'm being dumb.

  7. jklymak commented on Mar 28, 2018

    @jklymak
    Member

    #10910 fixes this, hopefully in a way that doesn't break anything else...

  8. modified the milestones: v3.0, v3.1 on Aug 11, 2018
  9. modified the milestones: v3.1.0, v3.2.0 on Mar 18, 2019
  10. dstansby commented on Apr 22, 2019

    @dstansby
    Member

    Closing, since #10910 is merged. Feel free to re-open or comment if this is still an issue.

  11. removed this from the v3.2.0 milestone on Apr 22, 2019
  12. tacaswell commented on Apr 22, 2019

    @tacaswell
    Member

    cleared the milestone as this was actually fixed in 3.0

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions