在matplotlib图形中插入png图像 [英] Insert a png image in a matplotlib figure
问题描述
我正在尝试在matplotlib图中插入png图像(
获得的输出:
我想知道如何调整为避免重叠而必须插入的图像的大小.
保存数字
ax.figure.savefig("output.svg",transparent = True,dpi = 600,bbox_inches ="tight")
您可以缩放图像,并将框对齐方式设置为右下角(0,1),外加一些额外的边距:
im = OffsetImage(arr_img,zoom = .45)ab = AnnotationBbox(im,(1,0),xycoords ='轴分数',box_alignment =(1.1,-0.1))
您可能还希望使用数据坐标(这是默认值),并使用默认的box_alignment到中心,例如 ab = AnnotationBbox(im,(2.6,1.45))
.有关各种坐标选项的更多信息,请参见 xycoords参数文档./p>
I'm trying to insert a png image in matplotlib figure (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()
Inset image:
Output obtained:
I'd like to know how to resize the image that has to be inserted to avoid overlaps.
EDIT: Saving the figure
ax.figure.savefig("output.svg", transparent=True, dpi=600, bbox_inches="tight")
You can zoom the image and the set the box alignment to the lower right corner (0,1) plus some extra for the margins:
im = OffsetImage(arr_img, zoom=.45)
ab = AnnotationBbox(im, (1, 0), xycoords='axes fraction', box_alignment=(1.1,-0.1))
You may also want to use data coordinates, which is the default, and use the default box_alignment to the center, e.g. ab = AnnotationBbox(im, (2.6, 1.45))
. See the xycoords parameter doc for more information about various coordinate options.
这篇关于在matplotlib图形中插入png图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!