Skip to content

savefig PDF output - incorrect annotation location #10844

@delorytheape

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.

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy