DMelt:Plots/Real-Time Data
From HandWiki
Revision as of 10:07, 14 February 2021 by imported>Jworkorg
Member
Showing real-time data
All canvas can be used to show data updated in real time. As example, we show how to display a random points, updating the data every 100 ms. Note that we use the method "clearData()", which reloads data container but does not change its graphical attributes. A slower method is "clearAll()" which reloads graphical attributes of the canvas.
from java.awt import Color from java.util import Random from java.lang import Thread from jhplot import HPlot,H1D import time c1 = HPlot("Canvas",600,400) c1.setGTitle("Histogram from stream of data (in real time)") c1.visible(1) c1.setLegend(0) #c1.setAutoRange() c1.setRange(-3,3,0,30) h1 = H1D("Gaussian numbers every 0.5 sec",20, -2.0, 2.0) h1.setFill(1) h1.setErrX(0) h1.setErrY(1) h1.setFillColorTransparency(0.7) h1.setFillColor(Color.blue) h1.setColor(Color.blue) h1.setPenWidthErr(2) rand = Random() for i in range(100): h1.fill(rand.nextGaussian()) time.sleep(0.1) c1.draw(h1) c1.drawStatBox(h1) time.sleep(0.5) #c1.clearData()
The output is shown here:
The method "clearData()"
may produce some memory leak if labels for X or Y are should be drawn. It is advised to call "clearLabels()" method, in addition to "clearData()" to avoid a memory leak related to the font creation/destruction of labels. |
You can also use a lighter canvas, "SPlot", which is much simpler and requires less resources.
DataMelt has a special canvas called jhplot.HPlotRT which is designed to show data
in real time and fast dynamic rendering. Look at this code:
from jhplot import * from java.awt import Color from java.util import Random from info.monitorenter.gui.chart.traces import Trace2DSimple import time trace = Trace2DSimple() c1 = HPlotRT("Canvas") trace1 = Trace2DSimple("Trace1") trace1.setColor(Color.red) c1.add(trace1) trace2 = Trace2DSimple("Trace2") c1.add(trace2) rand = Random() for i in range(100): time.sleep(0.05) trace1.addPoint(i,rand.nextGaussian()) trace2.addPoint(i, 5+rand.nextGaussian())
The output image is below: