get_sample_data 在绘图中插入图像时来自 Matplotlib

get_sample_data from Matplotlib when inserting an image in a plot

我正在处理一个项目,在我的代码中(使用 python)我想在我创建的其中一个绘图中插入一个图像。我发现我可以使用 Matplotlib 中的 get_sample_data 插入图像,但文件(图像)必须位于特定目录中(\matplotlib\mpl-data\sample_data) ,问题是这个项目必须在其他计算机上 运行,所以我想将绘图的图像放在与代码相同的目录中(也就是说,\documents\bs).我发现答案是 rc,但我不是编程专家,我不太确定它是如何工作的……所以我想知道是否有人知道我如何解决我的问题,或者给我建议一些在哪里看的参考资料,因为我找不到那么多关于它的信息。

感谢已进!

有人问我,我附上部分代码:

wave.plot(xstring,ystring,color='brown',linewidth=2.0)
xy = (0.5, 0.7)
left = get_sample_data("./lside_hand.png", asfileobj=False)
right = get_sample_data("./rside_hand.png", asfileobj=False)
arr_l=read_png(left)
arr_r=read_png(right)
imageboxl = OffsetImage(arr_l, zoom=0.1)
imageboxr = OffsetImage(arr_r, zoom=0.1)
ab_l = AnnotationBbox(imageboxl, xy,xybox=(-107., 0))
ab_r = AnnotationBbox(imageboxr, xy,xybox=(107., 0))
wave.add_artist(ab_l)
wave.add_artist(ab_r)

wave 是一个已经定义好的图,没有问题,xstring 和 ystring 也是数组中的一些数据。我对这些事情没有任何问题,也没有收到错误。我遇到的问题是函数 get_sample_data,每当我将文件保存在另一个不是 sample_data 的目录中时,都会给我一个错误(说找不到文件)。

更清楚地说,现在我将文件放在那个目录中,并且可以工作,但是如果我将它发送给其他人,he/she 必须将这些文件包含在那个目录中,这很可惜...我创建的情节是:

Plot I created

您应该阅读 matplotlib (link here) 的图像教程的一部分,因为它对您很有帮助。但简而言之,要显示图像,首先要抓取或生成图像,然后使用生成图形的方法。

如果图像是 png,您可以使用 matplotlib 的 imread() 获取图像,使用 pyplot 的 imshow()show 将图像显示到您的显示器

>>> import matplotlib.image as mpimg
>>> import matplotlib.pyplot as plt
>>> image = mpimg.imread('/home/nana20/documents/bs/myimage.png')
>>> plt.imshow(image)   # Generates image
>>> plt.show()          # Sends the image to display (your monitor)