Del*_*ory 5 python matplotlib legend
我希望使用 annotate() 在 Matplotlib 图中显示一些文本,在图例框下方对齐。我已经检查了如何在 matplotlib 中直接在图例下方放置文本框中提出的解决方案?并且可以做出适合我的情况的调整,但是添加额外的轴等对于我的情况来说似乎有点过分。我只是希望在图例框的右下角下方放置一个右上角为 20 像素的注释。我还希望保存情节的 PDF 文件。
这是我的代码。
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()
Run Code Online (Sandbox Code Playgroud)
当绘图首次显示时,它看起来是正确的,图例下方的信息文本正确。此外,保存的 png 文件显示正确。但是,pdf 输出中缺少文本。
我不确定我的理解是否存在差距,或者是否有问题。谁能解释为什么pdf输出不正确?
png 输出
pdf输出
(matplotlib=2.2,python=2.7.13,Windows 7)
| 归档时间: |
|
| 查看次数: |
1549 次 |
| 最近记录: |