# Texts' positions and sizes in variable-sized pads

**URL:** <https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822>\
**Category:** Newbie\
**Tags:** root\
**Created:** [May 3, 2022, 1:34pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822 "2022-05-03T13:34:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Y.S.Zhang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/cab0a1/32.png) [@Y.S.Zhang](https://root-forum.cern.ch/u/Y.S.Zhang)\
**Post date:** [May 3, 2022, 1:34pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/1 "2022-05-03T13:34:30Z")

</div>

Dear ROOT experts and developers,

I wonder if ROOT will provide a function to set the title offset of each axis by pixel or canvas margin. A related post is [here](https://root-forum.cern.ch/t/histogram-title-offset-in-pixel-and-canvas-margins/26633/7).

I would like to align the axes’ titles across different-sized pads. An example will be [here](https://root-forum.cern.ch/t/axis-labels-overlapped-by-pad/6920/28). The output plot show texts in different sizes by eye.

Another [post](https://root-forum.cern.ch/t/labels-dropping-off-using-canvaspartition/26097/5) mentioned [TRatioPlot](https://root.cern.ch/doc/master/classTRatioPlot.html). I tried to looked at the source code to understand how to achieve this goal. I can only get the idea that it re-draws the axes. But I cannot really understand how the properties of axes are adjusted.

Will you please provide any suggestions, how to properly set the positions of titles which can be the same as or different from the way in `TRatioPlot`?

* * *

_Please read [tips for efficient and successful posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292) and [posting code](https://root-forum.cern.ch/t/posting-code-read-this-first/28293)_

_ROOT Version:_ Not Provided  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<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:** [May 3, 2022, 1:36pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/2 "2022-05-03T13:36:51Z")

</div>

Do you have an example showing the problem ?

---

<div class="post-metadata">

**Author:** ![Y.S.Zhang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/cab0a1/32.png) [@Y.S.Zhang](https://root-forum.cern.ch/u/Y.S.Zhang)\
**Post date:** [May 3, 2022, 1:55pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/3 "2022-05-03T13:55:10Z")

</div>

Hi, I borrowed an example [plot](https://root-forum.cern.ch/uploads/default/optimized/2X/e/ea1ce4b0817d9f940f40845ebc4d99570c608dda_2_415x500.png) from an old post.

To make my question clear, can I have any functions to align the titles’ texts (here are the y axes) without setting the offset of each axis’s title by eye? I wonder if I can set just one pad/frame and the others can automatically be adapted.

`TRatioPlot` is great. It aligns the titles across upper and lower pads. I wonder how can I mimic this class. My naive idea is to set the title offset using pixel, which seems not feasible now.

---

<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:** [May 3, 2022, 1:57pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/4 "2022-05-03T13:57:04Z")

</div>

> [@Y.S.Zhang](#):
>
> My naive idea is to set the title offset using pixel, which seems not feasible now.

Exactly, the offset is a normalised value. If you send me your code I can try to make it look nicer.

---

<div class="post-metadata">

**Author:** ![Y.S.Zhang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/cab0a1/32.png) [@Y.S.Zhang](https://root-forum.cern.ch/u/Y.S.Zhang)\
**Post date:** [May 3, 2022, 2:03pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/5 "2022-05-03T14:03:38Z")

</div>

Thanks for the kind reply!

Would ROOT provide this functionality, to override the title offset using pixel, in the future? Or will ROOT release a generic version of `TRatioPlot`, to provide histogram frames only, in the future? This will ease the pain when people try to plot on pads with different sizes.

---

<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:** [May 3, 2022, 2:06pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/6 "2022-05-03T14:06:14Z")

</div>

This is not foreseen. But can you provide me an example showing what you are doing ? That would help me to understand what to do. I am not sur to fully understand. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Y.S.Zhang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/cab0a1/32.png) [@Y.S.Zhang](https://root-forum.cern.ch/u/Y.S.Zhang)\
**Post date:** [May 3, 2022, 3:00pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/7 "2022-05-03T15:00:46Z")

</div>

```auto
void makeplot() {
  float r = 0.3;
  float epsilon = 0.02;

  // histograms preparations
  TH1F *h1 = new TH1F("h1", "test1", 100, -3, 3);
  h1->SetStats(0);
  h1->GetXaxis()->SetLabelSize(0.);
  h1->GetXaxis()->SetTitleSize(0.);
  h1->FillRandom("gaus", 200000);
  h1->SetMinimum(0.);

  TH1F *h2 = new TH1F("h2", "test2", 100 , -3, 3);
  h2->SetStats(0);
  h2->FillRandom("gaus", 100000);

  // a canvas
  TCanvas *c1 = new TCanvas("c1", "example", 600, 700);

  // pads
  // use epsilon to make sure 0 is properly visualized, not truncated
  TPad *pad1 = new TPad("pad1", "pad1", 0, r-epsilon, 1, 1);
  pad1->SetBottomMargin(epsilon);

  // upper y is not r - epsilon
  TPad *pad2 = new TPad("pad2", "pad2", 0, 0, 1, r*(1-epsilon));
  pad2->SetTopMargin(0);
  pad2->SetFillColor(0);
  pad2->SetFillStyle(0);

  // leave enough spaces for title
  pad1->SetLeftMargin(0.14);
  pad2->SetLeftMargin(0.14);
  pad2->SetBottomMargin(0.2);

  // start to draw
  c1->cd();
  pad1->Draw();
  pad1->cd();
  h1->Draw();
  c1->cd();
  pad2->Draw();
  pad2->cd();
  h2->Draw("ep");

  // you can stop here and comment the below.
  // Check the output and compare with the one after manipulations below

  // manipulate the histograms
  h1->GetYaxis()->SetTitle("h1");
  h2->GetYaxis()->SetTitle("h2");

  // scale factor in y direction, pad1 / pad2
  double scale_y = (1-r+epsilon)/r/(1-epsilon);
  double scale_x = 1;
  // these are easy to scale
  h2->GetYaxis()->SetLabelSize(scale_x * scale_y * h1->GetYaxis()->GetLabelSize());
  h2->GetYaxis()->SetTitleSize(scale_x * scale_y * h1->GetYaxis()->GetTitleSize());

  h2->GetXaxis()->SetLabelSize(scale_x * scale_y * h2->GetXaxis()->GetLabelSize());
  h2->GetXaxis()->SetTickLength(scale_y * h1->GetXaxis()->GetTickLength());

  // title offset is hard to control.
  // I have to set the offset manually,
  // otherwise it hits the margin of the canvas.
  // Can I have a way to set the offset using some functions/calculations?
  // h2->GetYaxis()->SetTitleOffset(0.7);

  // What TRatioPlot is are shown below.
  // The titles are not aligned perfectly, but it is better than the above.
  TCanvas *c2 = new TCanvas("c2", "ratio plot", 600, 700);
  TH1D* hgaus = new TH1D("hgaus", "", 60, -3, 3);
  hgaus->FillRandom("gaus");
  hgaus->Fit("gaus");
  TRatioPlot* rp = new TRatioPlot(hgaus);
  rp->SetSeparationMargin(0);
  c2->Draw();
  rp->Draw();
  rp->GetLowerRefYaxis()->SetTitle("ratio");
  rp->GetUpperRefYaxis()->SetTitle("entries");
  rp->GetLowerPad()->SetLeftMargin(0.14);
  rp->GetUpperPad()->SetLeftMargin(0.14);
}

```

Would this snippet of example code help? I have a few comments in the macro and I hope they can explain my question.

As posted in the comment of the macro, how can I determine the title offset of the lower pad without by eye?

My naive answer is to make plot by pixel, which is not possible now. Can I request such a function in the future?

`TRatioPlot` is not perfect but still acceptable. Can we have a class provide the histogram frames of `TRatioPlot` without restricting the lower pad for ratio? And maybe this proposed class can provide more pads, rather than just upper and lower pads?

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:** [May 3, 2022, 3:20pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/8 "2022-05-03T15:20:35Z")

</div>

```auto
void makeplot() {
  float r = 0.3;
  float epsilon = 0.02;

  // histograms preparations
  TH1F *h1 = new TH1F("h1", "Global title", 100, -3, 3);
  h1->SetStats(0);
  h1->FillRandom("gaus", 200000);
  h1->SetMinimum(0.);

  TH1F *h2 = new TH1F("h2", "", 100 , -3, 3);
  h2->SetStats(0);
  h2->FillRandom("gaus", 100000);

  // a canvas
  TCanvas *c1 = new TCanvas("c1", "example", 600, 700);

  // pads
  // use epsilon to make sure 0 is properly visualized, not truncated
  TPad *pad1 = new TPad("pad1", "pad1", 0, r-epsilon, 1, 1);
  pad1->SetBottomMargin(epsilon);

  // upper y is not r - epsilon
  TPad *pad2 = new TPad("pad2", "pad2", 0, 0, 1, r*(1-epsilon));
  pad2->SetTopMargin(0);

  // leave enough spaces for title
  pad1->SetLeftMargin(0.14);
  pad2->SetLeftMargin(0.14);
  pad2->SetBottomMargin(0.2);

  // start to draw
  c1->cd();
  pad1->Draw();
  pad1->cd();
  h1->Draw();
  c1->cd();
  pad2->Draw();
  pad2->cd();
  h2->Draw("ep");

  // manipulate the histograms
  h1->GetYaxis()->SetTitle("h1");
  h2->GetYaxis()->SetTitle("h2");

  // Labels and titles in pixel
  h1->GetXaxis()->SetLabelFont(43);
  h1->GetXaxis()->SetLabelSize(16);
  h1->GetYaxis()->SetLabelFont(43);
  h1->GetYaxis()->SetLabelSize(16);
  h2->GetXaxis()->SetLabelFont(43);
  h2->GetXaxis()->SetLabelSize(16);
  h2->GetYaxis()->SetLabelFont(43);
  h2->GetYaxis()->SetLabelSize(16);

  h1->GetYaxis()->SetTitleFont(43);
  h1->GetYaxis()->SetTitleSize(20);
  h2->GetYaxis()->SetTitleFont(43);
  h2->GetYaxis()->SetTitleSize(20);

  // Remove first Label of the top plot.
  h1->GetYaxis()->ChangeLabel(1,-1,0,-1,-1,-1,"");
}

```

 ![Screenshot 2022-05-03 at 17.19.58](https://root-forum.cern.ch/uploads/default/original/3X/9/5/952596ad7f7554d460d9ecfcddd97dfaab40d548.png)

---

<div class="post-metadata">

**Author:** ![Y.S.Zhang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/cab0a1/32.png) [@Y.S.Zhang](https://root-forum.cern.ch/u/Y.S.Zhang)\
**Post date:** [May 3, 2022, 3:36pm UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/9 "2022-05-03T15:36:01Z")

</div>

Thank you for this elegant solution! One follow-up question is, which line enables the pixel setting? I checked the [documentation](https://root.cern/doc/v624/classTAttAxis.html#a59684f3441f945f1b2d4e6970ad3d1b5) for `TAttAxis::SetLabelSize`, and it says

> [@](#):
>
> The size is expressed in per cent of the pad size.

---

<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:** [May 4, 2022, 5:18am UTC](https://root-forum.cern.ch/t/texts-positions-and-sizes-in-variable-sized-pads/49822/10 "2022-05-04T05:18:53Z")

</div>

The [precision 3 in the text font](https://root.cern/doc/master/classTAttText.html#ATTTEXT5).  
Your are right, I updated the [TAttAxis doc](https://github.com/root-project/root/pull/10519).
