# Multi Canvas Graph

Dear rooters,

I need to plot two different graphs on each canvas. The plot is incremental (I read a txt file and each point is plotted on different canvas). Here is the simplified version of my code (with “for” loop):

``````       TCanvas c1;
c1.Divide(1,2);

TGraph	gr;

for(int i=0;i<10;i++)
{
// here I plot y = x^2 graph on first sub canvas

c1.cd(1);
gr.SetPoint(i,i,i*i);
gr.Draw("AC");

// here I want to plot y = 10x - x^2 on the second canvas, using the same constructor gr

c1.cd(2);
gr.SetPoint(i,i,10*i - i*i);
gr.Draw("AC");
}

c1.Print("plots.pdf");   // create a pdf file``````

The resulting plot is dominated only by the last content (10i - i*i) and what I get is two canvases with the same figures.

Is there any way to “plot the first content, save/freeze it on the first sub canvas” and then continue with the same constructor by resetting its content toward the second content and plot on the second sub canvas. And so on.

Basically, I need N sub canvases (according to data), so I cannot define new independent TGraph constructors in advance.

I will appreciate any help and suggestions.

Best,

Andrey.

That’s perfectly normal because you are drawing the same graph in the two pads.
use 2 graphs.

[quote=“couet”][quote]
what I get is two canvases with the same figures.
[/quote]

That’s perfectly normal because you are drawing the same graph in the two pads.
use 2 graphs.[/quote]

You are indeed correct, but what I need is to draw TWO distinct plots on each pad. Now the question is how to change my code in order to get that.

``````{
TGraph   gr1;
TGraph gr2;

for(int i=0;i<10;i++)  {
// here I plot y = x^2 graph on first sub canvas
c1.cd(1);
gr1.SetPoint(i,i,i*i);
gr1.Draw("AC");

// here I want to plot y = 10x - x^2 on the second canvas, using the same constructor gr

c1.cd(2);
gr2.SetPoint(i,i,10*i - i*i);
gr2.Draw("AC");
}

c1.Print("plots.pdf");   // create a pdf file``````

[quote=“couet”][code]
{
TGraph gr1;
TGraph gr2;

``````    for(int i=0;i<10;i++)  {
// here I plot y = x^2 graph on first sub canvas
c1.cd(1);
gr1.SetPoint(i,i,i*i);
gr1.Draw("AC");

// here I want to plot y = 10x - x^2 on the second canvas, using the same constructor gr

c1.cd(2);
gr2.SetPoint(i,i,10*i - i*i);
gr2.Draw("AC");
}
``````

c1.Print(“plots.pdf”); // create a pdf file
[/code][/quote]

You are absolutely right, but my case is a bit different: the number of “gr’s” (gr1, gr2) is not previously known. I read a txt file and plot the points and only at the end of the txt file I may know how many “gr’s” are used.

Using the example in above, imagine you have n-different function I want to plot and this n is not know in the begging. Theoretically, I can define “the array of gr[j]”, but there should be more simple solution.

Yes there is a simpler way. Create a pointer to a TGraph:

``TGraph *gr;``

And in the Loop do

`````` gr = new TGraph(n,x,y)
c1->cd(i);
gr->Draw("AC");``````

[quote=“couet”][quote]

``` gr = new TGraph(n,x,y) c1->cd(i); gr->Draw("AC"); ```[/quote][/quote]

Thank you for your effort, but we are talking about different things.

I do not know a priori the length “n” of the arrays x,y, but rather a set of arriving data (read a txt file). Here is a better example:

Assume, that you read a point (w; x,y) (input from txt file) and if:

w<0, you should plot the pair (x,y) it on the first pad
0<w<2, plot it on the second pad,
2<w<4 plot it on the third pad,
4<w<10 plot in the fourth pad
and so on…

BUT! you do not know apriori, how many pads you will need (maby four, five ten…), actually it is possible to limit the number by some big number 100, 1000. It’s OK.

After you reach the end of the txt file, you will get 4 pads with different graphs according to this sorting.

No problem use SetPoint():
root.cern.ch/root/html/TGraph.ht … h:SetPoint

``````gr = new TGraph();

while-there-is-new-points do {
gr->SetPoint(j++,x,y);
}

c1->cd(i);
gr->Draw("AC");``````

[quote=“couet”]
No problem use SetPoint():
root.cern.ch/root/html/TGraph.ht … h:SetPoint

[code]
gr = new TGraph();

while-there-is-new-points do {
gr->SetPoint(j++,x,y);
}

c1->cd(i);
gr->Draw(“AC”);
[/code][/quote]

Thank you a lot! Works like a charm! 