How to 'recalculate' TMultiGraph Axes

Hello,

(I suspect I have the same problem stated here, but I’m not sure, and anyway is an old post for an old version. I’m using 6.08/06, but I can upgrade if it solves the problem).
If I do (I try in interactive mode for simplicity, but inside scripts it doesn’t seem different):

g = new TGraph()
g->SetPoint(0,1,2)
g->Draw("AP")
g->SetPoint(1,6,9)

When the canvas is updated the axis are adjusted considering both points, and I can see the Graph correctly.

Now, I would like to do the same with a MultiGraph (that is nice exactly because calculates the axes for me): I first plot some graphs with some points (in the example here, it will be only one) in a TMultiGraph; then I look at the result, and basing on that I decide how to further populate the graphs (and so on): but the axes are not updated!

a = new TMultiGraph("a","a")
g = new TGraph()
g->SetPoint(0,1,2)
a->Add(g)
a->Draw("AP")
g->SetPoint(1,6,9)

If the new point happened to be inside the old axis range, it is shown.

Is there nowadays a correct way to do what I would like to? In the linked question a workaround is presented by the asker:

using copies of all TGraph objects. Then when it is necessary to add a new graph I delete TMultiGraph object, create new one and add all graphs to it in one place

but certainly there will be a better way?

Thank you!
Concetto

I do not see the problem. if I run the following macro I see all the points added to the graph after the multigraph has been drawn:

{
   a = new TMultiGraph("a","a");
   g = new TGraph();
   g->SetPoint(0,1,2);
   g->SetMarkerStyle(20);
   a->Add(g);
   a->Draw("AP");
   g->SetPoint(1,6,9);
   g->SetPoint(2,6,20);
}

Hm, you are right: if I run that same macro, all points are shown. I guess it means that the Graph is not actually plotted until the end, regardless of where we ask “->Draw()” in the script. Compare with running:

{
   c = new TCanvas("c","c");
   a = new TMultiGraph("a","a");
   g = new TGraph();
   g->SetPoint(0,1,2);
   g->SetMarkerStyle(20);
   a->Add(g);
   a->Draw("AP");
   g->SetPoint(1,6,9);
   c->Update(); //Now it's drawn.
   g->SetPoint(2,6,20); //Axes not updated.
}

And that’s why, if I try your exact sequence typing the commands one by one in interactive (canvas immediately updated), the axes are not updated either.
So, the axes are chosen the first time the canvas is updated.

When I run you macro I get the fooling image. Which is correct.

Indeed! My output in 6.08/06:

Thank you, I updated to the last production release and now I get your same result. I guess something was fixed between 6.08/06 and 6.10/00.

However, a last problem persists (luckily, one that allows for an easy workaround). If another canvas is active when adding a point:

{
   c = new TCanvas("c","c");
   a = new TMultiGraph("a","a");
   g = new TGraph();
   g->SetPoint(0,1,2);
   g->SetMarkerStyle(20);
   a->Add(g);
   a->Draw("AP");
   g->SetPoint(1,6,9);
   c->Update();
   
   c2 = new TCanvas("c2","c2");
   //Irrelevant plots on c2 here.
   
   //c->cd(); //Using this solves the problem. Commenting it:
   g->SetPoint(2,6,20); //Axes are stuck now, until e.g. a new point is added "properly".
}

This gives the same output of the last picture.
Without TMultiGraph, one does not need c->cd() : this works:

{
   c = new TCanvas("c","c");
   g = new TGraph();
   g->SetPoint(0,1,2);
   g->SetMarkerStyle(20);
   g->Draw("AP");
   g->SetPoint(1,6,9);
   c->Update();
   
   c2 = new TCanvas("c2","c2");
  
   g->SetPoint(2,6,20);
}

The way to do it would be:

{
   c = new TCanvas("c","c");
   a = new TMultiGraph("a","a");
   g = new TGraph();
   g->SetPoint(0,1,2);
   g->SetMarkerStyle(20);
   a->Add(g);
   a->Draw("AP");
   g->SetPoint(1,6,9);
   c->Update();

   c2 = new TCanvas("c2","c2");

   g->SetPoint(2,6,20);
   c->Modified();
   c->Update();
}

Thank you, that makes sense and works.

Just a side point. I thought that on interactive mode (after script exited) clicking/etc on the canvas would automatically issue the ‘Modified’ (otherwise I don’t understand why, giving all the commands on interactive, the axes are updated without need of c->Modified().). But:

If you try to run my script, when it exits the axes are not updated (no matter how much I click on the canvas).
Hey, they don’t change even if you now (in interactive, after script is over) call c->Modified(). Only adding a new point triggers the updating. (Not a big issue, I will just be careful to always call Modified before script end, but it’s still odd, no?)

Yes a “Modified” and “Update” is issued each time the prompt is displayed but it is done on gPad only . Here you have created c2 which is gPad when th macro finishes its execution, and the graphics you want to update is on “c” not on gPad (gPad - c2).

Right, I see.
Thanks!

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