Is it possible to patch an image in matplotlib?

半城伤御伤魂 提交于 2021-01-29 03:02:50

问题


I'm developing and automata in Python with matplotlib, and I would like to design it with a robot-look I picked on the web. I chose a file and I would like to place it in place of the black squares in the image below...

I have been looking for a way to do it on the web but I haven't found any answer. FYI, I use the fig = plt.Figure() method and then the fig.add_subplot to create my subplot and I finally generate the black square by creating black patches.


回答1:


I don't believe patches are meant for this purpose. However, since you undoubtedly know the location and bounding area of the black boxes, OffsetImage and AnnotationBbox is a viable alternative.

import math
import numpy as np
from matplotlib.offsetbox import OffsetImage, AnnotationBbox

x = np.linspace(0,10, 10)
y = [math.sin(i) for i in x]
fig, ax = plt.subplots()
im = plt.imread('pacman.png')
oi = OffsetImage(im, zoom = 0.15)
a = []
for px, py in zip(x,y):
    box = AnnotationBbox(oi, (px, py), frameon=False)
    a.append(ax.add_artist(box))
ax.plot(x,y,'r--')

Hope this helps.



来源:https://stackoverflow.com/questions/41453902/is-it-possible-to-patch-an-image-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!