# Two adjacent graphs

**URL:** <https://root-forum.cern.ch/t/two-adjacent-graphs/13676>\
**Category:** ROOT\
**Created:** [January 27, 2012, 10:03am UTC](https://root-forum.cern.ch/t/two-adjacent-graphs/13676 "2012-01-27T10:03:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phidelio](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@phidelio](https://root-forum.cern.ch/u/phidelio)\
**Post date:** [January 27, 2012, 10:03am UTC](https://root-forum.cern.ch/t/two-adjacent-graphs/13676/1 "2012-01-27T10:03:17Z")

</div>

I want to plot a TGraph and TF1 under each other, so that they have a common x axis. I saw a very good solution here

> [@Locate like command](https://root-forum.cern.ch/t/locate-like-command/65/1):
>
> Hi everybody, I’m just learning how to use root interface. There is a way to have a graphical integral of an histogram (like the PAW command “locate -i”) ?? cheers Paolo Zuccon.

The problem is that I want the lower pad to be three times smaller than the upper one. So, I had to modify a little the script provided there. I have attached my version. It draws the two pads in the right ratio, but on top of each other.

I appreciate any help.

---

<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 27, 2012, 10:17am UTC](https://root-forum.cern.ch/t/two-adjacent-graphs/13676/2 "2012-01-27T10:17:20Z")

</div>

> [@](#):
>
> I have attached my version. […]

The attachement is missing…

---

<div class="post-metadata">

**Author:** ![phidelio](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@phidelio](https://root-forum.cern.ch/u/phidelio)\
**Post date:** [January 27, 2012, 11:57am UTC](https://root-forum.cern.ch/t/two-adjacent-graphs/13676/3 "2012-01-27T11:57:46Z")

</div>

Oh, I hate when I forget the attachments.

Since I get the following error

> [@](#):
>
> The upload was rejected because the uploaded file was identified as a possible attack vector.

I will only copy the contents of the macro.

[code]void graph()  
{  
//Draw a simple graph  
// To see the output of this macro, click begin\_html here. end\_html  
//Author: Rene Brun

```
TCanvas *c1 = new TCanvas("c1","multipad",900, 700);
TPad *pad1 = new TPad("pad1","",0,0.3,1,1);
pad1->Draw();
pad1->cd();

```

// c1-\>Divide(1, 2, 0, 0);

```
const Int_t n = 4;
Double_t x[n]= {0.3156, 1.0012, 1.4146, 8.4996};
Double_t y[n]= {1.881, 5.899, 8.376, 50.239};

c1->cd(1);
gr = new TGraph(n,x,y);
gr->SetLineColor(2);
gr->SetLineWidth(4);

```

// gr-\>SetMarkerColor(4);  
gr-\>SetMarkerStyle(3);  
gr-\>SetTitle(“Energy calibration”);  
gr-\>GetXaxis()-\>SetTitle(“Relative amplitude”);  
gr-\>GetYaxis()-\>SetTitle(“Energy [keV]”);  
gr-\>Draw(“AP”);

```
//linear dependence
TF1 *f1 = new TF1("f1","5.899*x",0,9);

```

// f1-\>Draw(“same”);

```
//fit quadratic
TF1 *f2 = new TF1("f2", fitf,0, 9, 2);
f2->SetParameters(-0.01, 5.9);
gr->Fit(f2,"0");
f2->Draw("same");

//define a function as difference between quadratic fit and 5.899*x
TF1 *f3 = new TF1("f3", fitf,0, 9.3, 2);
f3->SetParameter(1,0);
f3->SetParameter(0,f2->GetParameter(0));

```

//compute the pad range with suitable margins  
TPad _pad2 = new TPad(“pad2”,“”,0,0,1,0.3);  
Double\_t ymin = -0.025;  
Double\_t ymax = 0;  
Double\_t dy = (ymax-ymin)/0.8; //10 per cent margins top and bottom  
Double\_t xmin = 0;  
Double\_t xmax = 9.3;  
Double\_t dx = (xmax-xmin)/0.8; //10 per cent margins left and right  
pad2-\>Range(xmin-0.1_dx,ymin-0.1_dy,xmax+0.1_dx,ymax);  
pad2-\>Draw();  
pad2-\>cd();

```
f3->Draw();

```

// pad2-\>Update();

// // TCanvas::Update() draws the frame, after which one can change it  
// c1-\>Update();  
// c1-\>GetFrame()-\>SetFillColor(21);  
// c1-\>GetFrame()-\>SetBorderSize(12);  
// c1-\>Modified();  
}

Double\_t fitf(Double\_t \*x, Double\_t \*par)  
{  
return x[0]\*x[0]\*par[0]+x[0]\*par[1];  
}  
[/code]

---

<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 27, 2012, 2:00pm UTC](https://root-forum.cern.ch/t/two-adjacent-graphs/13676/4 "2012-01-27T14:00:23Z")

</div>

do:

```auto
[...]
   c1->cd(); // <<<<<<<<<<<<<<<<<<<<<<<
   TPad *pad2 = new TPad("pad2","",0,0,1,0.3);
[...]
```

---

<div class="post-metadata">

**Author:** ![phidelio](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@phidelio](https://root-forum.cern.ch/u/phidelio)\
**Post date:** [January 27, 2012, 9:34pm UTC](https://root-forum.cern.ch/t/two-adjacent-graphs/13676/5 "2012-01-27T21:34:35Z")

</div>

Thank you. This was obvious, I should have seen it myself. Can you please help get rid of the margins between the two pads?

---

<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 30, 2012, 7:51am UTC](https://root-forum.cern.ch/t/two-adjacent-graphs/13676/6 "2012-01-30T07:51:12Z")

</div>

Change the pad position to reduce the top margins.
