在Matplotlib/Python中将小照片/图像添加到大型图形中

在Matplotlib/Python中将小照片/图像添加到大型图形中,python,matplotlib,Python,Matplotlib,我在matplotlib中生成了一个大型图形。我想在这个图的某些(x,y)坐标处添加一些图标。我想知道在matplotlib中是否有这样做的方法 谢谢这绝对有可能。这是一个开始: import matplotlib, scipy fig = matplotlib.figure() ax = fig.add_axes([0.1,0.1,0.8,0.8]) axicon = fig.add_axes([0.4,0.4,0.1,0.1]) ax.plot(range(5), [4,2,3,5,1])

我在matplotlib中生成了一个大型图形。我想在这个图的某些(x,y)坐标处添加一些图标。我想知道在matplotlib中是否有这样做的方法


谢谢

这绝对有可能。这是一个开始:

import matplotlib, scipy
fig = matplotlib.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
axicon = fig.add_axes([0.4,0.4,0.1,0.1])
ax.plot(range(5), [4,2,3,5,1])
axicon.imshow(scipy.randn(100,100))
axicon.set_xticks([])
axicon.set_yticks([])
fig.show()


在本例中,图标的位置未根据绘图的(x,y)坐标定义;也许其他人能帮上忙。尽管如此,我还是希望这个例子是有帮助的。

最初请参见-部分,但它展示了如何在不同坐标的绘图中添加矢量图形。

很多年后,但供将来参考。
我发现添加一个带有
OffsetImage
AnnotationBbox
,对我来说效果不错。e、 g

#!/usr/bin/env python
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt
from matplotlib.offsetbox import (OffsetImage,AnnotationBbox)
from matplotlib.cbook import get_sample_data
# The slices will be ordered and plotted counter-clockwise.
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
explode = (0.1, 0, 0, 0)  # explode a slice if required
fig, ax = plt.subplots()
ax.pie(sizes, explode=explode, labels=labels, colors=colors,
        autopct='%1.1f%%', shadow=True)
#Full path name to your image
fn = get_sample_data("/home/rolf/bandwidth/bandwidth.png", asfileobj=False)
arr_img = plt.imread(fn, format='png')

imagebox = OffsetImage(arr_img, zoom=1.2)
imagebox.image.axes = ax
xy = [0.75, 0.95]
ab = AnnotationBbox(imagebox, xy,
                    xybox=(120., -10.),
                    xycoords='data',
                    boxcoords="offset points",
                    pad=0.5,
                    )
ax.add_artist(ab)

# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.show()
使用
xy
xybox
值在绘图内移动图像

可能有用的进一步阅读: