Get values from several plots in a script

Hello everyone,
I’m writing a script to get data from a plot window in a Groovy script (or javascript, or Python/Jython if needed, but not a macro).
The plot window is already open, and may contain more than one graph.
I’ve managed to get the active PlotWindow and test if it’s really what it should be. Now I would like to get the values of all the graphs, but I cannot figure out how to do that. PlotWindow.GetPlot() always returns data from a single graph.
Here’s the skeleton of the code:

import ij.*
import ij.IJ
import ij.WindowManager
// Some plot window should be selected before running
window = WindowManager.getCurrentWindow()
if(!(window instanceof ij.gui.PlotWindow)) throw new IllegalArgumentException(window.getTitle() + " is not a plot window.")
plot = window.getPlot()
(xValues, yValues) = [plot.getXValues(), plot.getYValues()]

IJ.log("" + yValues[0])

The last line is just for checking which plot has been selected and each time, it is the “first” one, i.e. the plot that was already present in the window before another one was added.
Any idea someone? Maybe I have to iterate over the elements of the plot window?
Thank you for your help!

Hi Nicolas,
Maybe the method float[][] getDataObjectArrays(int index) of Plot is what you’re looking for? The api-doc of the class Plot says:

‘‘Get the data of the n-th Plot Object containing xy data in the sequence they were added (Other Plot Objects such as labels, arrows, lines, shapes and hidden PlotObjects are not counted).’’

There is also getNumPlotObjects if you want to know how many plot-objects there are.

Best regards,
Volker

1 Like

Thank you @volker.
I wasn’t aware of this function, and I didn’t notice that a plot can actually store several curves, labels, etc. For the record, here’s the code that I ended up with:

window = WindowManager.getCurrentWindow()
if(!(window instanceof ij.gui.PlotWindow)) throw new IllegalArgumentException(window.getTitle() + " is not a plot window.")
plot = window.getPlot()
xyValues = new ArrayList()
String[] dataObjects = plot.getDataObjectDesignations() // The only function I found for getting references to the curves in the plot
dataObjects.eachWithIndex  {object, index->
	IJ.log(object)
	xyValues.add(plot.getDataObjectArrays(index))
}

xyValues now stores x, y, x error bars and y errors bars, if any, for each curve in the plot.