Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a small photo/image to a large graph in Matplotlib/Python

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

like image 570
Mark Avatar asked Jun 09 '10 04:06

Mark


People also ask

How do you change the size of a figure in Matplotlib?

Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.


3 Answers

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.

like image 58
Steve Tjoa Avatar answered Oct 04 '22 20:10

Steve Tjoa


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

like image 33
Jouni K. Seppänen Avatar answered Oct 04 '22 22:10

Jouni K. Seppänen


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/

like image 38
Rolf of Saxony Avatar answered Oct 04 '22 20:10

Rolf of Saxony