# 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.

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@…287…]
Sent: Sunday, June 10, 2007 5:22 PM
To: matplotlib-users@lists.sourceforge.net
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.

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

···

On 6/10/07, __ <redshifted@...287...> 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

Excellent. Thank you very much!

···

On 6/10/07, Jake Emerson <jake.emerson@…1306…> 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@…287…]
Sent: Sunday, June 10, 2007 5:22 PM
To: matplotlib-users@lists.sourceforge.net
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.