## matplotlib-users

 [Matplotlib-users] Simple scatter plot over an image From: __ - 2007-06-10 23:21:48 Attachments: Message as HTML ```Hello, I'm trying to plot a simple list of x/y coords over an image (.png). I can show the image, or plot the data, but cannot find a way to layer one over the other. I would greatly appreciate someone pointing me in the right direction. Thanks. ```
 Re: [Matplotlib-users] Simple scatter plot over an image From: Jake Emerson - 2007-06-11 03:10:18 Attachments: Message as HTML ```The python imaging library is pretty good for this kind of thing. =20 http://www.pythonware.com/library/pil/handbook/ =20 Here's an (untested) example. Hope it helps. =20 Jake =20 =20 #!/usr/bin/env python =20 from pylab import scatter, save import Image =20 #get the background image, and find out how big it is im_bg =3D Image.open("background.png") bg_width, bg_height =3D im.size =20 #make a white canvas on which to paste the background image and the scatter plot #this will allow you to, say, have the x- and y-axis values fall outside of the background image's limits im_canvas =3D Image.new("RGBA", (bg_width+60, bg_height+60), (255, 255, 255, 0)) =20 #create the scatter plot from x and y data with matplotlib scatter(x, y, s=3Dsizes, alpha=3D0.75) =20 #save the scatter plot, and then retrieve it for use in PIL, convert to RGBA so that alpha levels will work #there is probably a better way to do this with gcf() or gci()... savefig("scatter_plot.png") im_scatter =3D Image.open("scatter_plot.png").convert("RGBA") =20 #resize the scatter image to make it fit nice im_scatter.resize((bg_width+10, bg_height+10)) =20 #bring all of the images together by pasting them onto the white canvas, use the overlayed image as the mask (third element) im_canvas.paste(im_bg, (30, 30), im_bg) #play around with the paste locations (30, 30) im_canvas.paste(im_scatter, (10, 30), im_scatter) #these won't be perfect the first time (10, 30) =20 #save it im_canvas.save("combo_image.png") =20 =20 =20 =20 ________________________________ From: __ [mailto:redshifted@...]=20 Sent: Sunday, June 10, 2007 5:22 PM To: matplotlib-users@... Subject: [Matplotlib-users] Simple scatter plot over an image Hello,=20 I'm trying to plot a simple list of x/y coords over an image (.png). I can show the image, or plot the data, but cannot find a way to layer one over the other. I would greatly appreciate someone pointing me in the right direction. Thanks.=20 ```
 Re: [Matplotlib-users] Simple scatter plot over an image From: __ - 2007-06-11 13:08:06 Attachments: Message as HTML ```Excellent. Thank you very much! On 6/10/07, Jake Emerson wrote: > > The python imaging library is pretty good for this kind of thing. > > http://www.pythonware.com/library/pil/handbook/ > > Here's an (untested) example. Hope it helps. > > Jake > > > #!/usr/bin/env python > > from pylab import scatter, save > import Image > > #get the background image, and find out how big it is > im_bg = Image.open("background.png") > bg_width, bg_height = im.size > > #make a white canvas on which to paste the background image and the > scatter plot > #this will allow you to, say, have the x- and y-axis values fall outside > of the background image's limits > im_canvas = Image.new("RGBA", (bg_width+60, bg_height+60), (255, 255, 255, > 0)) > > #create the scatter plot from x and y data with matplotlib > scatter(x, y, s=sizes, alpha=0.75) > > #save the scatter plot, and then retrieve it for use in PIL, convert to > RGBA so that alpha levels will work > #there is probably a better way to do this with gcf() or gci()... > savefig("scatter_plot.png") > im_scatter = Image.open("scatter_plot.png").convert("RGBA") > > #resize the scatter image to make it fit nice > im_scatter.resize((bg_width+10, bg_height+10)) > > #bring all of the images together by pasting them onto the white canvas, > use the overlayed image as the mask (third element) > im_canvas.paste(im_bg, (30, 30), im_bg) #play around > with the paste locations (30, 30) > im_canvas.paste(im_scatter, (10, 30), im_scatter) #these won't be > perfect the first time (10, 30) > > #save it > im_canvas.save("combo_image.png") > > > > > ------------------------------ > *From:* __ [mailto:redshifted@...] > *Sent:* Sunday, June 10, 2007 5:22 PM > *To:* matplotlib-users@... > *Subject:* [Matplotlib-users] Simple scatter plot over an image > > Hello, > > I'm trying to plot a simple list of x/y coords over an image (.png). I can > show the image, or plot the data, but cannot find a way to layer one over > the other. I would greatly appreciate someone pointing me in the right > direction. Thanks. > ```
 Re: [Matplotlib-users] Simple scatter plot over an image From: John Hunter - 2007-06-11 12:33:00 ```On 6/10/07, __ wrote: > Hello, > > I'm trying to plot a simple list of x/y coords over an image (.png). I can > show the image, or plot the data, but cannot find a way to layer one over > the other. I would greatly appreciate someone pointing me in the right Just call imshow and set the "extent" kwarg to let mpl know about the coordinates of the image, and then call scatter. See examples/image_demo2.py in the mpl src distribution, ot at http://matplotlib.sourceforge.net/examples/image_demo2.py JDH ```