Plot multiple lines of best fit onto an image


I am trying to plot lines of best fit for multiple arrays of coordinates onto an image however I can’t find a good way to do it. The line ROI method doesn’t allow you to plot an entire array, just the start and end points of the line and the plot method opens up another window and creates a graph there. Is there a good way to do this? Any help would be appreciated.


Hi, @mconfused

If you have an image and want to plot a bunch of curves for which you have the X and Y coordinate arrays, you should have a look at the following:

Hope this helps,


Hi @oburri,

Thanks for the information, am I correct in thinking that these are Macro commands? I should have added I am writing a Java plugin and am trying to do this programatically.



How about creating a PolygonRoi ?

This is what the macro recorder records (in Java mode) when you draw a segmented line:

int[] xpoints = {119,60,140,227};
int[] ypoints = {87,165,212,147};
imp.setRoi(new PolygonRoi(xpoints,ypoints,4,Roi.POLYLINE));

Hi @mconfused ,

ij.process.ImageProcessor class has drawLine(int x1, int y1, int x2, int y2) method, so you could use this to iterate through the point coordinates and draw plot segments one by one.

pseudocode below, after setting draw color,

ip = IJ.getImage().getProcessor();
for ( i =0 ; i < xpoints.length -1; i++)
   ip.drawLine( xpoints[i] , ypoints[i], xpoints[i+1], ypoints[i+1]);