Optimize real time plotting of TGraph


I want to plot a TGraph in real time collecting data from serial.

Every time i execute my function i have A new data to add to the graph and i want to limit the size of the graph to Nmax values (circular array). My idea is to have a bigger arrays (size = 2*Nmax) and to handle them as shown:

Double_t * xArr = new Double_t[2 * Nmax ];
Double_t * yArr = new Double_t[2 * Nmax ];
int start = end = 0;
TGraph * realtimeG;

updateData(int A, int * x, float_t * y){
	for (int i = 0; i < A; i++){
		xArr[end] = (Double_t)x[i];
		yArr[end] = (Double_t)y[i];
		if (end - start > Nmax) start = end - Nmax; // move forward start
		if (end == 2 * Nmax){
			memcpy(xArr, xArr + Nmax, Nmax * sizeof(Double_t));
			memcpy(yArr, yArr + Nmax, Nmax * sizeof(Double_t));
			end = 0;
	// very time consuming, I als
	realtimeG->DrawGraph(end - start, RTt+start, RTa0+start, "C*");

	realtimeG = new TGraph(1);

The problem is that DrawGraph copy all data in fX and fY… I will probably try to implement something like

memcpy(realtimeG->GetX(), xArr + start, Nmax * sizeof(Double_t));
memcpy(realtimeG->GetY(), yArr + start, Nmax * sizeof(Double_t));

taking care that realtimeG always have Nmax points and copying the first aquired data on the array.

Obviously it would be easier if i have a GetX(Double_t *) and something to set fNpoints but I can’t find a simple way to do that.

Is there a better way to real time plot data without copying a lot of data every time?


May I suggest you to use the method SetPoint ? it allows to change any point of a TGraph. So your graph can stay of a fix size and you simply change the points inside.

Unfortunately in this way it’s very uncomfortable to build a “circular graph”. Once i have Nmax values in the graph I delete older points do add newer. With my (not working yet) solution i only have to copy 2 x Nmax values every 2Nmax values, cycling with SetPoint I have to copy Nmax-A points everytime I receive data.

Next try: I’ll extend TGraph to expose fX,fY and fNpoints, but it’s a more complex work I’d like to avoid

I my view a “circular graph” means that you have a graph with a fixed number of point of Nmax and when a new point come with an index greater the Nmax for instance Nmax+1 then you fill the position 1 in the graph … that what SetPoint does…

If you want to replace all the point in one go you better create a new graph … don’t you ?

Maybe I misunderstood the meanings… I want to move all data back A points and to add A data at the end, so i will always have the oldest data on the left of the array and new data on the right.

On the other hand, since I have time in X axis, the scatter results will be the same even if data is not ordered. The only problem is the connecting line that is missing from point y[Nmax] to y[0] and is present between newest and oldest data. Is there a way at least to remove a line only from the newest and oldest data?

In that case why not create a TMultigraph with two graph inside ? One with the old data and one with the new ones. They will draw together but will not be connected. It would be even possible to have different attitudes for each graph (different color for instance)… When new data will show you will just need to drop the graph you do not need from the multigraph and replace put the ne one in place.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.