# Multiple (similar) plots on Divided TCanvas

**URL:** <https://root-forum.cern.ch/t/multiple-similar-plots-on-divided-tcanvas/49004>\
**Category:** Newbie\
**Created:** [March 3, 2022, 10:35am UTC](https://root-forum.cern.ch/t/multiple-similar-plots-on-divided-tcanvas/49004 "2022-03-03T10:35:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [March 3, 2022, 10:35am UTC](https://root-forum.cern.ch/t/multiple-similar-plots-on-divided-tcanvas/49004/1 "2022-03-03T10:35:39Z")

</div>

Hi all,  
I am using PyROOT to plot mutiple (similar) histograms using TCanvas:: Divide():  
I used TF1 for testing (see the following code).

```auto
import ROOT as r
N = 2
canvas = r.TCanvas("canvas object name","function",600,400)
canvas.Divide(N,N)

for i in range(N*N):
    canvas.cd(i+1)
    cosx = r.TF1('cosx{}'.format(i),'cos(x+[0])',0,6.284)
# cosx = r.gDirectory.GetObject('cosx{}'.format(i))
    cosx.SetParameter(0,i)
    cosx.Draw()
canvas.Draw()

```

the output look like this:  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/e/1/e12995b38cd4ee4a7856c2d332753315fd606e1e.png)

I also tried the following (with no luck):

1. put `r.TF1(....)` outside the `for` loop, ending up with 4 identical plots.
2. uncomment the `gDirectory.Get` line, I got an error which says:  
Template method resolution failed: Failed to instantiate “GetObject(std::string)”

I am sure that other users also have encounterred the problem (but I really cant find a precise way to describe the problem, so I decide to ask in the Newbie section). Could somebody suggest the best way to do this kind of plot for me ? Thanks in advance.

\_ROOT Version: 6.24 (PyROOT via conda)  
\_Platform:Centos7  
\_Compiler: gcc9

* * *

---

<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:** [March 3, 2022, 10:42am UTC](https://root-forum.cern.ch/t/multiple-similar-plots-on-divided-tcanvas/49004/2 "2022-03-03T10:42:17Z")

</div>

[`cosx.DrawCopy()`](https://root.cern/doc/master/classTF1.html#ab71e20c7fa0e2625f92b5ea29ceca83e)

---

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [March 3, 2022, 10:46am UTC](https://root-forum.cern.ch/t/multiple-similar-plots-on-divided-tcanvas/49004/3 "2022-03-03T10:46:42Z")

</div>

Thanks. It worked !

---

<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:** [March 3, 2022, 10:49am UTC](https://root-forum.cern.ch/t/multiple-similar-plots-on-divided-tcanvas/49004/4 "2022-03-03T10:49:27Z")

</div>

Or you can use Draw(0 this way:

```auto
void junkai() {
   int N = 2;
   auto canvas = new TCanvas("canvas object name","function",600,400);
   canvas->Divide(N,N);

   for (int i=0; i<N*N; i++) {
      canvas->cd(i+1);
      auto cosx = new TF1(Form("cosx{%d}",i),"cos(x+[0])",0,6.284);
      cosx->SetParameter(0,i);
      cosx->Draw();
   }
}

```

 ![Screenshot 2022-03-03 at 11.48.41](https://root-forum.cern.ch/uploads/default/original/3X/1/b/1bb313e5b0af9f31e6c9f28b171ff5f29f343286.png)

---

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [March 3, 2022, 10:57am UTC](https://root-forum.cern.ch/t/multiple-similar-plots-on-divided-tcanvas/49004/5 "2022-03-03T10:57:32Z")

</div>

thanks for the suggestion. It worked in macro but not in python. Is there other method than the DrawCopy() in PyROOT (just curious) ?

Btw, I also found that the object that is created in the `for` loop cant be accessed by running: `gDirectory->ls();`. Outside the loop, I have a TH3 object and (of course) the canvas. But only the TH3 was listed when `gDirectory->ls();` was run. Am I using the correct way to access the list of created Objects?

```auto
void PhaseMapTest(){
	
	int NBinX = 18;
	int NBinY = 9;
	int NBinZ = 1000;
		
	TH3F* phase_map = new TH3F("phase_map","phase map title;posiInitposX [mm]; posiInitPosY [mm]; posiInitTime [#mu s]",NBinX,-45,45,NBinY,-45,45,NBinZ,0,149.2);
	phase_map->Sumw2();
	
	int x = 8;
	int y = 7;
	phase_map->GetXaxis()->SetRange(x+1,x+1);
	
	int N = 2;
   auto canvas = new TCanvas("canvas object name","function",600,400);
   canvas->Divide(N,N);

   for (int i=0; i<N*N; i++) {
      canvas->cd(i+1);
      auto cosx = new TF1(Form("cosx{%d}",i),"cos(x+[0])",0,6.284);
      cosx->SetParameter(0,i);
      cosx->Draw();
   };
	canvas->Print("t.png");
	gDirectory->ls();

```

---

<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:** [March 3, 2022, 11:14am UTC](https://root-forum.cern.ch/t/multiple-similar-plots-on-divided-tcanvas/49004/6 "2022-03-03T11:14:04Z")

</div>

`gROOT->GetListOfCanvases()->ls();`  
`gROOT->GetListOfFunctions()->ls();`
