<< Main Manual

Tutorial #2. Chart generation of time-series data

We have also incorporated the open source JFreeChart package that provides an extensive library of charting tools.
    This can be used to generate time-series plots within ImageJ. Specifically, one-dimensional waveforms or "Waves" can be generated by importing the data from imaging time-series ROIs or other sources and be manipulated with fine granularity.
    They can also be used to combine waves with differing sampling rates, such as electrophysiology data (next panel) along with imaging data that is probably at a slower sampling rate, and arbitrary waves that may have been manually generated with a certain sampling rate.
   These two examples illustrate two different ways of plotting waves. However, once the waves are available to the Wave Manager it is very easy to make further modifications like normalizing, scaling, or otherwise transforming the 1D data from the scripting environment like in Matlab or other mathematical software tools.

Example #1

Type in the following code in the Jython console to generate some chart data.

>>> # generate an image stack similar to before
>>> width = 125
>>> height = 250
>>> stack = ImageStack(width, height)
>>> for i in range(100):
...    ip = ByteProcessor(width, height)
...    ip.setColor(int(Math.sin(0.1*i)*256))
...    ip.fill()
...    stack.addSlice(ip)
...
>>> image = ImagePlus("ImageStack1", stack)
>>> image.show()
>>>
>>> # now create an ROI on the stack and plot the data in a chart
>>> xPoints = [10, 15, 10]
>>> yPoints = [10, 15, 20]
>>> roi = PolygonRoi(xPoints, yPoints, len(xPoints), Roi.POLYGON)
>>> roiManager.addRoi(roi)
>>> waveManager.importWaves()
>>>
>>> # select and rename the imported wave for easier access later
>>> waveManager.select(0)
>>> waveManager.rename("wave1")
>>> waveManager.plot()
>>>

This generates an image stack as found here (open in PhysImage) and a plot that looks similar to the following:
Wave Plot #1

To get the chart, use the WindowManager to retrieve the list of open charts. With this you can resize the window or customize the charts using the JFreeChart API.

>>> charts = WindowManager.getCharts()
>>> print charts
>>> # The 'chart' is actually an instance of ChartFramePlugIn
>>> #    Therefore, it can be manipulated like a normal Frame object from the Java API
>>> charts[0].setSize(400,200)
>>> charts[0].setLocation(100,100)
>>>
>>> # To access the JFreeChart you can use the following
>>> jfreechart = charts[0].getChart()
>>> # or for XYPlot
>>> plot = charts[0].getXYPlot()
>>> plot.getDomainAxis().setRange(0, 5)
>>> plot.getRangeAxis().setRange(0.5, 1)
>>>

These operations can be simplified as follows:

>>> # FrontChart() provides a means of affecting the "front chart"
>>> FrontChart().
setSize(600,300)
>>> FrontChart().setLocation(200,200)
>>>
>>> # modify the x- and y-axes by their axis labels or 'bottom' for the x-axis
>>> FrontChart().setAxisRange("bottom", 0, 10)
>>> FrontChart().setAxisRange("wave1", 25, 125)

>>>

Here is an example of how to programmatically annotate the chart.

>>> from org.jfree.chart.annotations import XYTextAnnotation
>>> annotation = XYTextAnnotation("test", 1.5, 0.75)
>>> plot.addAnnotation(annotation)

Example #2

Time-series data is managed in terms of "Waves". Each wave has an associated sampling rate that can be used so that different dataset with variable acquisition rates can be easily combined. Waves can be imported from time-series images as above, loaded from text files, or generated programmatically as below.

>>> wave = Wave()
>>> wave.setName("Sine")
>>> wave.setSamplingRateHz(100)
>>> for i in range(999):
...    wave[i] = Math.sin(0.05*i)
...
>>> wave2 = Wave()
>>> wave2.setName("Cosine")
>>> wave2.setSamplingRateHz(50)
>>> for i in range(500):
...    wave2[i] = Math.cos(0.05*i)
...
>>> waveManager.addWave(wave)
>>> waveManager.addWave(wave2)
>>> plot = waveManager.plot()

This generates a plot that looks like the following:
WavePlot2_1

The following example demonstrates a few other shortcuts for manipulating key features of the charts.

>>> FrontChart().setWaveStyle("Cosine", "area")
>>> FrontChart().setWaveColor("Cosine", Color(255, 0, 0))
>>> FrontChart().setAxisExtents("Sine", 0, 0.75)
>>> FrontChart().setAxisExtents("Cosine", 0.75, 1)
>>> FrontChart().setWaveColor("Sine", Color(0, 200, 0))
>>> FrontChart().setAxisRange("time (s)", 0, 7)
>>>

This changes the plot so that it looks like this:
WavePlot2_2

<< Tutorial #1. Python scripting control of ImageJ/PhysImage
>> Tutorial #3. Importing electrophysiological data

<< Main Manual