【发布时间】:2020-12-21 04:20:27
【问题描述】:
我正在尝试在 matplotlib 图中插入 png 图像 (ref)
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.figure import Figure
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
ax = plt.subplot(111)
ax.plot(
[1, 2, 3], [1, 2, 3],
'go-',
label='line 1',
linewidth=2
)
arr_img = plt.imread("stinkbug.png")
im = OffsetImage(arr_img)
ab = AnnotationBbox(im, (1, 0), xycoords='axes fraction')
ax.add_artist(ab)
plt.show()
插图:
得到的输出:
我想知道如何调整必须插入的图像的大小以避免重叠。
编辑: 保存图
ax.figure.savefig("output.svg", transparent=True, dpi=600, bbox_inches="tight")
【问题讨论】:
标签: python-3.x image matplotlib plot png