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

发布于 2024-09-05 01:29:06 字数 100 浏览 4 评论 0原文

我有一个在 matplotlib 中生成的大图。我想在该图中的某些 (x,y) 坐标处添加一些图标。我想知道在 matplotlib 中是否有任何方法可以做到这一点

谢谢

I have a large graph that I am generating in matplotlib. I'd like to add a number of icons to this graph at certain (x,y) coordinates. I am wondering if there is any way to do that in matplotlib

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-09-12 01:29:06

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

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) 坐标定义;也许其他人可以帮忙。尽管如此,我希望这个例子能有所帮助。

It's definitely possible. Here is a start:

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

Icon overlap http://up.stevetjoa.com/iconoverlap.png

In this example, the icon's position was not defined in terms of the plot's (x,y) coordinates; maybe someone else can help with that. Nevertheless, I hope this example is helpful.

机场等船 2024-09-12 01:29:06

请参阅海豚示例 - 最初是一个笑话,但它展示了如何在不同坐标的图中添加矢量图形。

See the dolphin example — originally a joke, but it shows how to add vector graphics in plots at various coordinates.

卖梦商人 2024-09-12 01:29:06

晚了很多年,但供将来参考。
我发现添加一个带有 OffsetImageAnnotationBbox 对我来说很有效。例如,

#!/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/

Many years late but for future reference.
I found that adding an AnnotationBbox with an OffsetImage in it, did the trick for me. 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()

Move the image about within the plot with xy and xybox values.

Further reading that may be useful:
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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文