-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
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;
- 'plot1.png', which is generated correctly
- '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.