# 2 histograms in one canvas

**URL:** <https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703>\
**Category:** General Chat\
**Created:** [January 18, 2009, 8:05pm UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703 "2009-01-18T20:05:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![florian](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://root-forum.cern.ch/u/florian)\
**Post date:** [January 18, 2009, 8:05pm UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/1 "2009-01-18T20:05:43Z")

</div>

hay  
I have two histograms, which I obtain by reading data from the file output\_file1.txt which is attached. Then I would like to plot both histograms in one canvas (but with different color). Unfortunately the histograms have a different scale. This can be seen if you read all the data points in one histogram (then you will see that the disks are in the middle of the halos). So one of the histograms does not follow the scale on the axis. Where is my mistake?  
I run the program with

.x pos\_plot.C

A second point which occurs in some of my root files is, that I can execute the program only once. If I try it again I get a segmentation fault. I have to leave root and start it again. Probably I wrote bad code and forgot anything, but what?  
thanks for any help  
best regards  
florian

 ![](https://root-forum.cern.ch/uploads/default/original/2X/5/5fc1b63afcb5426b72aaddf5b0d6ae9543d473d1.jpg)  
  
[output\_file1.txt](https://root-forum.cern.ch/uploads/default/original/2X/9/9a9f2d622a727145a8c98e4eb7f6cdcfa91b5686.txt) (1.8 MB)  
[pos\_plot.C](https://root-forum.cern.ch/uploads/default/original/2X/4/4352a06791a7036953c7d48723d115a5add65a37.C) (947 Bytes)

---

<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:** [January 19, 2009, 9:14am UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/2 "2009-01-19T09:14:35Z")

</div>

here is a small example showing how to draw 2 TGraph2D with the same scale on the same plot:

```auto
void graph2dsame2()
{
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(1111);

   TCanvas *c = new TCanvas("c","Graph2D example",0,0,800,800);

   TH2F *h1 = new TH2F("h1","h1",10,0,10,10,0,10);
   h1->SetDirectory(0);
   h1->SetMinimum(0);
   h1->SetMaximum(10);

   Double_t x1[4] = {1,1,2,2};
   Double_t y1[4] = {1,2,1,2};
   Double_t z1[4] = {1,2,3,4};

   TGraph2D *g2d1 = new TGraph2D(4,x1,y1,z1);
   g2d1->SetHistogram(h1);
   g2d1->Draw("P0 TRI1");

   TH2F *h2 = new TH2F("h2","h2",10,0,10,10,0,10);
   h2->SetDirectory(0);
   h2->SetMinimum(0);
   h2->SetMaximum(10);

   Double_t x2[4] = {5,5,6,6};
   Double_t y2[4] = {5,6,5,6};
   Double_t z2[4] = {5,6,7,8};

   TGraph2D *g2d2 = new TGraph2D(4,x2,y2,z2);
   g2d2->SetHistogram(h2);
   g2d2->Draw("P0 TRI1 SAME");
}
```

---

<div class="post-metadata">

**Author:** ![florian](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://root-forum.cern.ch/u/florian)\
**Post date:** [January 19, 2009, 9:59am UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/3 "2009-01-19T09:59:02Z")

</div>

hay, thanks for the answer…  
Is this the easiest method? Because I would like to define the axis automatically by the data. A scale from 0 to 10 is not very helpful. Of cause I can read in the maximum value of the data and use this for the histograms, but I fear the histogram max and min values have to be integer? I would like to be independent of histograms and stay with TGrapg2D  
I implemented your solution, and it is working, but only with some constrains and I fear it is not portable to similar problems.  
Is there a solution which just tells the second Graph to follow the scale of the first? (Actually I thought is is default, so maybe I made a mistake somewhere???)  
best regards  
florian

---

<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:** [January 19, 2009, 10:14am UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/4 "2009-01-19T10:14:18Z")

</div>

in the line:

```auto
   TH2F *h1 = new TH2F("h1","h1",10,0,10,10,0,10); 
```

only the number of bin is integer. the other parameter are double and you can pout the value you want. see:[http://root.cern.ch/root/html/TH2.html](http://root.cern.ch/root/html/TH2.html)

---

<div class="post-metadata">

**Author:** ![florian](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://root-forum.cern.ch/u/florian)\
**Post date:** [January 20, 2009, 3:55pm UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/5 "2009-01-20T15:55:23Z")

</div>

hay  
I would like to have only points “P” in the plot like show in the picture above. In your example you use TRI1 which gives a surface. If I delete TRI1 and use only “P” or “P0” as draw option the axis of the histogram are not plotted???  
my modified program looks at the moment like this

```auto
#include <math.h>

void pos_plot(){
   gROOT->SetStyle("Plain");
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(1111);

   const int num = 1000000;
   double x[num], y[num], z[num], type[num], dummy;
   double xmax, ymax, zmax;
   int n = 0;
   int n2 = 0;

   xmax = 0;
   ymax = 0;
   zmax = 0;

   ifstream widthfile("output_file1.txt");
   while((widthfile >> x[n] >> y[n] >> z[n] >> type[n])){
      if(xmax < x[n]) xmax = x[n];
      if(ymax < y[n]) ymax = y[n];
      if(zmax < z[n]) zmax = z[n];
      n++;
   }  
   widthfile.close();

   cout<<"xmax: "<<xmax<< endl;
   cout<<"ymax: "<<ymax<< endl;
   cout<<"zmax: "<<zmax<< endl;

   TCanvas *c1 = new TCanvas("c1", "pos_plot", 0, 0, 750, 600);    

   TH2F *h1 = new TH2F("h1","h1",10,-ymax,ymax,10,-zmax,zmax);
   h1->SetDirectory(0);
   h1->SetMinimum(-xmax);
   h1->SetMaximum(xmax);

   h1->SetTitle("");

   TGraph2D *g2d1 = new TGraph2D(100,x,y,z);
   g2d1->SetHistogram(h1);
   g2d1->Draw("P");

   TH2F *h2 = new TH2F("h2","h2",10,-ymax,ymax,10,-zmax,zmax);
   h2->SetDirectory(0);
   h2->SetMinimum(-xmax);
   h2->SetMaximum(xmax);

   TGraph2D *g2d2 = new TGraph2D(2,x,y,z);
   g2d2->SetHistogram(h2);
   g2d2->Draw("P SAME");
}
```

thank you so much for helping  
best regards  
florian

---

<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:** [January 21, 2009, 9:12am UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/6 "2009-01-21T09:12:02Z")

</div>

Yes I see the problem I have no answer yet. Meanwhile I can propose you a other solution. As it seems you need a could of points it does not seems really necessary to use TGraph2D . 3D polymarkers are enough. The following example does what you want:

```auto
{
   gStyle->SetOptStat(0);

   TH3F *frame3d = new TH3F("frame3d","frame3d",10,-1,1,10,-1,1,10,-1,1);
   frame3d->Draw();

   TPolyMarker3D *pm3d1 = new TPolyMarker3D(3);
   pm3d1->SetPoint(0,1,0,0);
   pm3d1->SetPoint(1,0,1,0);
   pm3d1->SetPoint(2,0,0,1);

   pm3d1->SetMarkerSize(2);
   pm3d1->SetMarkerColor(kBlue);
   pm3d1->SetMarkerStyle(20);

   pm3d1->Draw();

   TPolyMarker3D *pm3d2 = new TPolyMarker3D(3);
   pm3d2->SetPoint(0,-1,0,0);
   pm3d2->SetPoint(1,0,-1,0);
   pm3d2->SetPoint(2,0,0,-1);

   pm3d2->SetMarkerSize(2);
   pm3d2->SetMarkerColor(kRed);
   pm3d2->SetMarkerStyle(20);

   pm3d2->Draw();
}
```

---

<div class="post-metadata">

**Author:** ![florian](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://root-forum.cern.ch/u/florian)\
**Post date:** [January 21, 2009, 11:20am UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/7 "2009-01-21T11:20:27Z")

</div>

thanks a lot… thats exactly what I needed…  
best regards  
florian

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 9, 2011, 9:29am UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/8 "2011-06-09T09:29:12Z")

</div>

Greetings,

I am sorry for posting this question here. I am a newbie in both c++ and root and I have limited time unfortunately.

I read your peoblem, it’s the same that I want to do. But I already have my .root histograms taken from G4 and AIDA. The output of AIDA is .root.

Nevertheless I have open these two histo in one canvas to compare them. Just want to ask if I should write a c++ code for that or if I can do it easily from the browser?

many thanks in advance

---

<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:** [June 9, 2011, 9:41am UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/9 "2011-06-09T09:41:38Z")

</div>

> [@](#):
>
> Just want to ask if I should write a c++ code for that or if I can do it easily from the browser?

If you want to do what is describe here use the c++ program I posted.  
You sent too few details to help you further.

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 9, 2011, 12:19pm UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/10 "2011-06-09T12:19:02Z")

</div>

I have three (.root) files including histograms.

I want to put all these three histogtams in one canvas in order to comapre these three histograms.

Thank you aagin

---

<div class="post-metadata">

**Author:** ![baharan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e56c9b/32.png) [@baharan](https://root-forum.cern.ch/u/baharan)\
**Post date:** [June 9, 2011, 1:08pm UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/11 "2011-06-09T13:08:26Z")

</div>

Hi again,

I felt better to include my files as well.  
any of them includes 3 histograms. In this case I want to compare all number 2s with each other.

Thank you again  
[essai3.root](https://root-forum.cern.ch/uploads/default/original/2X/0/0e84fdc9e242c89f3b0c90f881bed5c4e1c3b1f0.root) (9.13 KB)  
[essai2.root](https://root-forum.cern.ch/uploads/default/original/2X/a/ad79d0bdfcb2453dd629381057a3f1bf8064880d.root) (9.74 KB)  
[essai1.root](https://root-forum.cern.ch/uploads/default/original/2X/8/83b3deaba6893e122a64887439d385c3d45c433f.root) (9.78 KB)

---

<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:** [June 9, 2011, 1:36pm UTC](https://root-forum.cern.ch/t/2-histograms-in-one-canvas/7703/12 "2011-06-09T13:36:23Z")

</div>

see my answer here:

> [@Opening two .root file in the same canvas](https://root-forum.cern.ch/t/opening-two-root-file-in-the-same-canvas/12562/4):
>
> You plot the first histogram then the 2nd one using the [option SAME](https://root.cern.ch/doc/master/classTHistPainter.html#HP060).
