I'm working on a project and in my code (using python) I want to insert an image in one of the plots I create. I found out that I could insert an image by using the get_sample_data from Matplotlib but that the file(image) has to be in a specific directory (\matplotlib\mpl-data\sample_data), the problem is that this project will have to be run on other computers, so I'd like to have the images of the plot in the same directory as I have the code (so to say, \documents\bs). I found that the answer is rc, but I'm not an expert in programming and I'm not so sure how that works...So I'd like to know if someone knows how I could solve my problem, or suggest me some references of where to look, as I could not find so much information about it.
Thanks is advanced!
As someone asked me, I attach the part of the code:
xy = (0.5, 0.7)
left = get_sample_data("./lside_hand.png", asfileobj=False)
right = get_sample_data("./rside_hand.png", asfileobj=False)
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))
You should read a portion of the image tutorial for matplotlib (link here) as it will be very helpful for you. But briefly, to display an image, you first grab or generate the image and then use a method for generating the figure.
If the image is a png you can use matplotlib's
imread() to get the image and pyplot's
show to display the image to your monitor
>>> 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)