gpt4 book ai didi

python - 如何在绘图中使用自定义png图像标记?

转载 作者:行者123 更新时间:2023-12-03 14:37:03 24 4
gpt4 key购买 nike

我想在散点图和折线图中使用客户标记。如何从PNG文件中制作自定义标记?

最佳答案

我不相信matplotlib可以自定义标记。有关自定义级别,请参见here,它远远低于您的需求。
作为替代方案,我编写了此混合代码,该混合代码使用figimage将图像放置在线点位置。

import matplotlib.pyplot as plt
import matplotlib.image as image

# constant
dpi = 72
# read in our png file
im = image.imread('smile.png')
image_size = im.shape[1], im.shape[0]

fig = plt.figure(dpi=dpi)
ax = fig.add_subplot(111)
# plot our line with transparent markers, and markersize the size of our image
line, = ax.plot((1,2,3,4),(1,2,3,4),"bo",mfc="None",mec="None",markersize=imageSize[0] * (dpi/ 96))
# we need to make the frame transparent so the image can be seen
# only in trunk can you put the image on top of the plot, see this link:
# http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg14534.html
ax.patch.set_alpha(0)
ax.set_xlim((0,5))
ax.set_ylim((0,5))

# translate point positions to pixel positions
# figimage needs pixels not points
line._transform_path()
path, affine = line._transformed_path.get_transformed_points_and_affine()
path = affine.transform_path(path)
for pixelPoint in path.vertices:
# place image at point, centering it
fig.figimage(im,pixelPoint[0]-imageSize[0]/2,pixelPoint[1]-imageSize[1]/2,origin="upper")

plt.show()
产生:
enter image description here

关于python - 如何在绘图中使用自定义png图像标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2318288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com