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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2318288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:21:34  来源:igfitidea点击:

How to use custom png image marker with plot?

pythonmatplotlib

提问by tputkonen

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

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

回答by Mark

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

我不相信 matplotlib 可以自定义这样的标记。请参阅此处了解自定义级别,这远远达不到您的需要。

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

作为替代方案,我编写了这个 kludge,它使用 figimage 将图像放置在线点位置。

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/[email protected]/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()

Produces:

产生:

enter image description here

在此处输入图片说明

回答by t gillespie

Following on from Mark's answer. I just thought I would add to this a bit because I tried to run this and it does what I want with the exception of actually displaying the icons on the graph. Maybe something has changed with matplotlib. It hasbeen 4 years.

继马克的回答之后。我只是想我会添加一点,因为我尝试运行它并且它做我想要的,除了在图表上实际显示图标。也许 matplotlib 发生了一些变化。它已经连续4年。

The line of code that reads:

读取的代码行:

ax.get_frame().set_alpha(0)

does not seem to work, however

然而,似乎不起作用

ax.patch.set_alpha(0)

does work.

确实有效。

回答by ImportanceOfBeingErnest

The other answer may lead to problems when resizing the figure. Here is a different approach, positionning the images inside annotation boxes, which are anchored in data coordinates.

另一个答案可能会导致调整图形大小时出现问题。这是一种不同的方法,将图像定位在标注框内,标注框锚定在数据坐标中。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox

path = "https://upload.wikimedia.org/wikipedia/commons/b/b5/Tango-example_icons.png"
image = plt.imread(path)[116:116+30, 236:236+30]

x = np.arange(10)
y = np.random.rand(10)

fig, ax = plt.subplots()
ax.plot(x,y)

def plot_images(x, y, image, ax=None):
    ax = ax or plt.gca()

    for xi, yi in zip(x,y):
        im = OffsetImage(image, zoom=72/ax.figure.dpi)
        im.image.axes = ax

        ab = AnnotationBbox(im, (xi,yi), frameon=False, pad=0.0,)

        ax.add_artist(ab)

plot_images(x, y, image, ax=ax)

plt.show()

enter image description here

在此处输入图片说明