# TCanvas not dividing

**URL:** <https://root-forum.cern.ch/t/tcanvas-not-dividing/38682>\
**Category:** Newbie\
**Created:** [April 3, 2020, 11:35am UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682 "2020-04-03T11:35:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 11:35am UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/1 "2020-04-03T11:35:54Z")

</div>

I am using root 5.34/38 (this is what is returned when I punch in gROOT-\>GetVersion() )

I have an array of 12 TGraphs called `g` and an array of 6 TMultiGraphs called `mg`. The goal is to plot two of the TGraphs in each of the 6 TMultiGraphs and plot all 6 multigraphs on a single page. I initialize the canvas with:

```auto
TMultiGraph* mg[6];
TGraph* g[12];
TCanvas *c1 = new TCanvas("c1","PSA Parameters",1800,2400);
c1->Divide(2,3);

```

Later, I make the TGraphs using methods that are not found in native ROOT (we have some custom software). The TGraphs are made just fine and are successfully stored in the array `g`.

To make all the multigraphs, I have:

```auto
for(int j = 0; j < 6; j++){
    c1->cd(j+1);
    mg[j] = new TMultiGraph();
    mg[j]->Add(g[j]);
    mg[j]->Add(g[j+6]);
    mg[j]->Draw("AP");
    mg[j]->SetTitle(Form("This is a dummy Title"));
}

gPad->BuildLegend();
gPad->SetGrid();
c1->SaveAs(Form("%s/Channel_%03d.pdf",strOutPath.Data(),ch));

```

However, when I plot this, I get a single TGraph in my pdf file, not 6. What am I missing?

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/3/2/328c8cafa61f6037ed424f471b60ba7d69a40ddd.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 3, 2020, 11:49am UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/2 "2020-04-03T11:49:20Z")

</div>

As it si written your macro will save only the last multigraph in the pdf file: mg[5]

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 3, 2020, 11:49am UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/3 "2020-04-03T11:49:33Z")

</div>

See if you do not “Draw” something in another place (note also that all “gPad” calls should go inside of the “for” loop).  
Before “`c1->SaveAs(...);`” add “`c1->cd(0);`”

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:07pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/4 "2020-04-03T12:07:51Z")

</div>

Yes, I see that, but can you tell me why please?

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:09pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/5 "2020-04-03T12:09:52Z")

</div>

I draw the individual TGraphs. That should be okay though, right?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 3, 2020, 12:12pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/6 "2020-04-03T12:12:31Z")

</div>

It depends where you draw them. Maybe you “overwrite” your canvas (it may “delete” all subpads).

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 3, 2020, 12:12pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/7 "2020-04-03T12:12:42Z")

</div>

you should produce the pdf files in the loop. A SaveAs after each Draw.

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:13pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/8 "2020-04-03T12:13:21Z")

</div>

I dont want a separate PDF for each instance of the loop. I want to save a single file with 6 different plots on it

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 3, 2020, 12:14pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/9 "2020-04-03T12:14:17Z")

</div>

Make a multigraph containing the 6 plots draw it and save it as a pdf

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:15pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/10 "2020-04-03T12:15:01Z")

</div>

That is also not what I want to do. I want 6 multigraphs, each of which contains 2 plots, displayed as a 3x2 grid on the canvas

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:17pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/12 "2020-04-03T12:17:12Z")

</div>

Are you even reading my original post? that’s in line 4 of the code I posted. This is exactly why I’m having a problem.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 3, 2020, 12:17pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/13 "2020-04-03T12:17:18Z")

</div>

Can you send a reproducer macro we can run ?

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:17pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/14 "2020-04-03T12:17:44Z")

</div>

No I cannot because the plots I am making are using experiment-specific software that you will not be able to run. Please read my original post.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 3, 2020, 12:17pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/15 "2020-04-03T12:17:45Z")

</div>

@couet What is the problem that you see in the original code in the first post?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 3, 2020, 12:18pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/16 "2020-04-03T12:18:22Z")

</div>

Ok give me some time I will make a reproducer

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:18pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/17 "2020-04-03T12:18:37Z")

</div>

Thank you very much.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 3, 2020, 12:21pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/19 "2020-04-03T12:21:31Z")

</div>

Before each "`g[i]->Draw("ap");` add “`c1->cd(i + 1);`” (otherwise you automatically “delete” all subpads).

Do not use “`gPad = c1->cd(j+1);`”, use simply “`c1->cd(j + 1);`” (“gPad” will automatically be updated).

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:22pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/20 "2020-04-03T12:22:04Z")

</div>

OHHHHHHHH that makes a lot of sense I shall try that now

---

<div class="post-metadata">

**Author:** ![kvetter](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a183cd/32.png) [@kvetter](https://root-forum.cern.ch/u/kvetter)\
**Post date:** [April 3, 2020, 12:24pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/21 "2020-04-03T12:24:30Z")

</div>

I managed to make this work by simply removing the g[j]-\>Draw(“ap”) lines. Thank you both for helping me with this. My sincerest gratitude to both of you.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 3, 2020, 12:33pm UTC](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682/22 "2020-04-03T12:33:44Z")

</div>

```auto
void kvetter()
{
   TMultiGraph* mg[6];
   TGraph* g[12];
   TCanvas *c1 = new TCanvas("c1","PSA Parameters",1800,2400);
   c1->Divide(2,3);

   for (int i = 0; i <12; i++) {
      g[i] = new TGraph();
      g[i]->SetPoint(0,i,i*i);
      g[i]->SetPoint(1,i,i*i+1);
      g[i]->SetMarkerStyle(20);
      g[i]->SetName(Form("Graph #%d",i));
   }

   for(int j = 0; j < 6; j++){
      c1->cd(j+1);
      mg[j] = new TMultiGraph();
      mg[j]->Add(g[j]);
      mg[j]->Add(g[j+6]);
      mg[j]->SetTitle(Form("This is a dummy Title #%d",j));
      mg[j]->Draw("AP");
      gPad->Update();
      gPad->SetGrid();
      gPad->BuildLegend();
   }

   c1->SaveAs("kvetter.pdf");
}

```

 ![Screenshot 2020-04-03 at 14.32.44](https://root-forum.cern.ch/uploads/default/original/3X/5/2/5261faf9369f00bb4d8fa3d4bdcc282e2fffa411.png)

[Next page](https://root-forum.cern.ch/t/tcanvas-not-dividing/38682.md?page=2)
