在Matplotlib/Python中向大图添加小照片/图片

5
我有一个在matplotlib中生成的大图表,我想在特定的(x,y)坐标处添加多个图标。我想知道是否有办法在matplotlib中实现这一点。
谢谢。
3个回答

6
肯定是可以的。以下是一个开始:

肯定是有可能的。

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

图标重叠 http://up.stevetjoa.com/iconoverlap.png

在这个例子中,图标的位置没有根据绘图的(x,y)坐标定义;也许其他人可以帮忙解决这个问题。尽管如此,我希望这个例子对你有所帮助。


1
多年之后,但供以后参考。
我发现在AnnotationBbox中添加一个OffsetImage可以解决我的问题。例如:
#!/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()

使用xyxybox值在图中移动图像。

更多相关阅读:
http://matplotlib.org/examples/pylab_examples/demo_annotation_box.html https://developer.ibm.com/clouddataservices/2016/10/06/your-own-weather-forecast-in-a-python-notebook/


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接