如何在绘图中使用自定义png图像标记? [英] How to use custom png image marker with plot?

查看:238
本文介绍了如何在绘图中使用自定义png图像标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to utilize customer markers in both scatter and line charts. How can I make custom marker out of a PNG file?

推荐答案

我不相信matplotlib可以自定义标记.有关自定义级别的信息,请参见此处,这远远超出了您的需求.

I don't believe matplotlib can customize markers like that. See here for the level of customization, which falls way short of what you need.

作为替代方案,我编写了此混合代码,该混合代码使用figimage将图像放置在线点位置.

As an alternative, I've coded up this kludge which uses figimage to place images at the line point locations.

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

# constants
dpi = 72; imageSize = (32,32)
# read in our png file
im = image.imread('smile.png')

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.get_frame().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()

产生:

这篇关于如何在绘图中使用自定义png图像标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆