Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you directly overlay a scatter plot on top of a jpg image in matplotlib / Python?

I need to rapidly plot jpg frames that result as the output of a tracking algorithm. Companion with the jpg frames are text files containing simple (x,y) data locating the image targets that are being tracked. I would like to use matplotlib to plot the jpg images, then overlay a scatter plot of the (x,y) data which gets read from the text file and stored into a Pythonic list. Below is code that will plot the jpg image, but in all of the scouring I have done of matplotlib, scipy, and PIL manuals and help pages, I cannot find anything that explains how to maintain this plot window and simply overlay a scatter plot of simple markers at various (x,y) locations in the image. Any help is greatly appreciated.

import matplotlib.pyplot as plt; im = plt.imread(image_name); implot = plt.imshow(im); plt.show() 
like image 983
ely Avatar asked Feb 22 '11 02:02

ely


People also ask

How do I overlay a scatterplot in matplotlib?

You simply call the scatter function twice, matplotlib will superimpose the two plots for you. You might want to specify a color, as the default for all scatter plots is blue. This is perhaps why you were only seeing one plot. Save this answer.

How do you make a scatter plot on a picture?

To create an image scatter plot, right-click the layer you want analyze in the Contents pane, point to Create Chart, and click Scatter plot to open the Chart Properties pane. To define the parameter settings for your chart, click the Data tab at the top of the Chart Properties pane.


2 Answers

The pyplot.scatter() function was tailor made for this reason:

import matplotlib.pyplot as plt im = plt.imread(image_name) implot = plt.imshow(im)  # put a blue dot at (10, 20) plt.scatter([10], [20])  # put a red dot, size 40, at 2 locations: plt.scatter(x=[30, 40], y=[50, 60], c='r', s=40)  plt.show() 

See the documentation for more info.

like image 81
lothario Avatar answered Oct 11 '22 11:10

lothario


this should work:

import matplotlib.pyplot as plt im = plt.imread('test.png') implot = plt.imshow(im) plt.plot([100,200,300],[200,150,200],'o') plt.show() 

keep in mind that each pixel in the image is one unit on the x,y axes. The 'o' is a shorthand way of getting the plot function to use circles instead of lines.

like image 32
Paul Avatar answered Oct 11 '22 11:10

Paul