# Two plotting aesthetics questions (TGraph and TAxis)

**URL:** <https://root-forum.cern.ch/t/two-plotting-aesthetics-questions-tgraph-and-taxis/61900>\
**Category:** Newbie\
**Created:** [October 10, 2024, 5:18am UTC](https://root-forum.cern.ch/t/two-plotting-aesthetics-questions-tgraph-and-taxis/61900 "2024-10-10T05:18:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_Flanagan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/will_flanagan/32/35050_2.png) [@Will\_Flanagan](https://root-forum.cern.ch/u/Will_Flanagan)\
**Post date:** [October 10, 2024, 5:18am UTC](https://root-forum.cern.ch/t/two-plotting-aesthetics-questions-tgraph-and-taxis/61900/1 "2024-10-10T05:18:45Z")

</div>

I have two questions relating to a plotting macro, code below and output pdf attached.

(1) When I draw TGraphs on a dummy TH2D (for formatting), why doesn’t my TGraph continue to the x-maximum of the plot range? As a corollary, I am puzzled why i have to subtract 1 in the TGraph range to make it properly align.

(2) How can I nudge up my second y-axis on the right side? For instance, is it possible to add blank spaces or tabs to the content of TAxis titles?

Many thanks in advance for the help.

-Will

[ActivityAndRates.pdf](https://root-forum.cern.ch/uploads/short-url/rF07p2QfjnVW0yq7A4DVsSd1lrx.pdf) (14.4 KB)  
[Rates.C](https://root-forum.cern.ch/uploads/short-url/hWVYrOX1MvKbqqJZC8xi0zMVdoO.C) (3.8 KB)

void Rates(){

TCanvas \*c1 = new TCanvas(“c1”,“”,700,500);

gStyle-\>SetOptStat(0);  
gStyle-\>SetLegendBorderSize(0);

TH2D \*hDummy = new TH2D(“hDummy”,“”,1000,0,37,1000,0,1.30);  
hDummy-\>Draw();  
hDummy-\>GetXaxis()-\>SetTitle(“Days”);  
hDummy-\>GetXaxis()-\>CenterTitle();  
hDummy-\>GetXaxis()-\>SetTitleSize(0.08);  
hDummy-\>GetXaxis()-\>SetTitleOffset(0.5);  
hDummy-\>GetYaxis()-\>SetTitle(“Activity [GBq]”);  
hDummy-\>GetYaxis()-\>CenterTitle();  
hDummy-\>GetYaxis()-\>SetTitleSize(0.08);  
hDummy-\>GetYaxis()-\>SetTitleOffset(0.5);

TGaxis \*axis = new TGaxis(37,0.0, 37, 1.15,0,1.5,51,“+L”);  
axis-\>SetNdivisions(505);  
axis-\>SetLabelFont(gStyle-\>GetLabelFont(“Y”));  
axis-\>SetTitleFont(gStyle-\>GetTitleFont(“Y”));  
axis-\>SetTitle(“Background Rate [MHz]”);  
axis-\>CenterTitle();  
axis-\>SetTitleSize(0.08);  
axis-\>SetTitleOffset(0.5);  
axis-\>Draw();

TLine \*line = new TLine(10.5,0,10.5,1.30);  
line-\>SetLineStyle(2);  
line-\>Draw(“same”);

double Act\_Zr88\_0 = 1.11; // GBq  
double tHalf\_Zr88 = 83.4; // days  
double tHalf\_Y88 = 106.629; // days  
double Eff\_Zr88 = 0.0002;  
double Eff\_Y88 = 0.0035;

TF1 \*fAct\_Zr88 = new TF1(“fAct\_Zr88”,“[0]\*exp(-log(2.0)\*x/[1])”,-1,200);  
fAct\_Zr88-\>SetParameters(Act\_Zr88\_0, tHalf\_Zr88);  
fAct\_Zr88-\>SetLineColor(kBlue+2);  
fAct\_Zr88-\>SetLineWidth(4);  
fAct\_Zr88-\>Draw(“same”);

TF1 _fAct\_Y88 = new TF1(“fAct\_Y88”,"(1.0/[1])/(1.0/[2]-1.0/[1])_[0]\*(exp(-log(2.0)\*x/[1])-exp(-log(2.0)\*x/[2]))",-1,200);  
fAct\_Y88-\>SetParameters(Act\_Zr88\_0, tHalf\_Zr88, tHalf\_Y88);  
fAct\_Y88-\>SetLineColor(kRed+2);  
fAct\_Y88-\>SetLineWidth(4);  
fAct\_Y88-\>Draw(“same”);

TF1 \*fAct\_Tot = new TF1(“fAct\_Tot”,“[0]_exp(-log(2.0)x/[1])+(1.0/[1])/(1.0/[2]-1.0/[1])[0]_(exp(-log(2.0)\*x/[1])-exp(-log(2.0)\*x/[2]))”,-1,200);  
fAct\_Tot-\>SetParameters(Act\_Zr88\_0, tHalf\_Zr88, tHalf\_Y88);  
fAct\_Tot-\>SetLineColor(kBlack);  
fAct\_Tot-\>SetLineWidth(4);  
fAct\_Tot-\>Draw(“same”);

TF1 _fBkg\_Zr88 = new TF1(“fBkg\_Zr88”,"1000_[3]\*[0]\*exp(-log(2.0)\*x/[1])",9.5,200);  
fBkg\_Zr88-\>SetParameters(Act\_Zr88\_0, tHalf\_Zr88, tHalf\_Y88, Eff\_Zr88);  
fBkg\_Zr88-\>SetLineColor(kBlue+2);  
fBkg\_Zr88-\>SetLineWidth(4);  
fBkg\_Zr88-\>SetLineStyle(7);  
fBkg\_Zr88-\>Draw(“same”);

TF1 _fBkg\_Y88 = new TF1(“fBkg\_Y88”,"1000_[4]_(1.0/[1])/(1.0/[2]-1.0/[1])_[0]\*(exp(-log(2.0)\*x/[1])-exp(-log(2.0)\*x/[2]))",9.5,200);  
fBkg\_Y88-\>SetParameters(Act\_Zr88\_0, tHalf\_Zr88, tHalf\_Y88, Eff\_Zr88, Eff\_Y88);  
fBkg\_Y88-\>SetLineColor(kRed+2);  
fBkg\_Y88-\>SetLineWidth(4);  
fBkg\_Y88-\>SetLineStyle(7);  
fBkg\_Y88-\>Draw(“same”);

TF1 _fBkg\_Tot = new TF1(“fBkg\_Tot”,"1000_[3]_[0]exp(-log(2.0)x/[1])+1000[4](1.0/[1])/(1.0/[2]-1.0/[1])_[0]\*(exp(-log(2.0)\*x/[1])-exp(-log(2.0)\*x/[2]))",9.5,200);  
fBkg\_Tot-\>SetParameters(Act\_Zr88\_0, tHalf\_Zr88, tHalf\_Y88, Eff\_Zr88, Eff\_Y88);  
fBkg\_Tot-\>SetLineColor(kBlack);  
fBkg\_Tot-\>SetLineWidth(4);  
fBkg\_Tot-\>SetLineStyle(7);  
fBkg\_Tot-\>Draw(“same”);

TLegend\* legZr88 = new TLegend(0.33, 0.81, .50, .89);  
legZr88-\>SetTextSize(0.04);  
legZr88-\>AddEntry(fAct\_Zr88, “^{88}Zr Activity”, “l”);  
legZr88-\>AddEntry(fBkg\_Zr88, “^{88}Zr Rate”, “l”);  
legZr88-\>SetMargin(0.3);  
legZr88-\>Draw(“same”);

TLegend\* legY88 = new TLegend(0.52, 0.81, .69, .89);  
legY88-\>SetTextSize(0.04);  
legY88-\>AddEntry(fAct\_Y88, “^{88}Y Activity”, “l”);  
legY88-\>AddEntry(fBkg\_Y88, “^{88}Y Rate”, “l”);  
legY88-\>SetMargin(0.3);  
legY88-\>Draw(“same”);

TLegend\* legTot = new TLegend(0.70, 0.81, .89, .89);  
legTot-\>SetTextSize(0.04);  
legTot-\>AddEntry(fAct\_Tot, “Total Activity”, “l”);  
legTot-\>AddEntry(fBkg\_Tot, “Total Rate”, “l”);  
legTot-\>SetMargin(0.3);  
legTot-\>Draw(“same”);

TLegend\* legStart = new TLegend(0.05, 0.02, .15, .06);  
legStart-\>SetTextSize(0.04);  
legStart-\>SetHeader(“5 AUG 24”);  
legStart-\>Draw(“same”);

TLegend\* legBeam = new TLegend(0.27, 0.02, .37, .06);  
legBeam-\>SetTextSize(0.04);  
legBeam-\>SetHeader(“16 AUG 24”);  
legBeam-\>Draw(“same”);

TLegend\* legEnd = new TLegend(0.85, 0.02, .95, .06);  
legEnd-\>SetTextSize(0.04);  
legEnd-\>SetHeader(“8 SEP 24”);  
legEnd-\>Draw(“same”);

c1-\>SaveAs(“ActivityAndRates.pdf”);  
}

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 10, 2024, 6:31am UTC](https://root-forum.cern.ch/t/two-plotting-aesthetics-questions-tgraph-and-taxis/61900/2 "2024-10-10T06:31:19Z")

</div>

For 1, not sure what you mean. Also, there are no TGraphs in your code.  
For 2, “How can I nudge up my second y-axis on the right side?” do you mean the _title_ of the axis (judging by the second question)? did you try simply adding the spaces to the title text?

```auto
axis->SetTitle(" Background Rate [MHz]");

```

---

<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:** [October 10, 2024, 6:51am UTC](https://root-forum.cern.ch/t/two-plotting-aesthetics-questions-tgraph-and-taxis/61900/3 "2024-10-10T06:51:02Z")

</div>

Your macro produces this plot:

 ![Screenshot 2024-10-10 at 08.44.31](https://root-forum.cern.ch/uploads/default/original/3X/1/6/16d2ca75bb023e326c1bffea324292af90bdc0ff.jpeg)

As @dastudillo said there is no TGraph in your code, only TF1. I do not see why you should have ranges problem. What’s wrong with the right title ? you want to center it on the axis ?  
(your axis titles seem a bit too big though). Also note that to draw an empty frame we have TPad::DrawFrame, and that the option “same” does not exist for TLine::Draw() it will be ignored.

---

<div class="post-metadata">

**Author:** ![Will\_Flanagan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/will_flanagan/32/35050_2.png) [@Will\_Flanagan](https://root-forum.cern.ch/u/Will_Flanagan)\
**Post date:** [October 11, 2024, 11:13pm UTC](https://root-forum.cern.ch/t/two-plotting-aesthetics-questions-tgraph-and-taxis/61900/4 "2024-10-11T23:13:27Z")

</div>

@dastudillo and @couet, thank you for your replies so far.

@couet, to your response ‘I do not see why you should have ranges problem.’ Please note that the TF1s must have a starting range as 9.5 in order to start at 10.5. Why is this? Also, please note that these functions do not continue to the right limit of the plot. Why is this? Are there simple ways to fix this?

@dastudillo, to your point, I had mistakenly thought that the leading blank spaces in my title were being ignored. I see now that I was wrong. Apologies.

My apologies also for mixing up TGraph with TF1.

Thanks,  
Will

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 12, 2024, 5:02am UTC](https://root-forum.cern.ch/t/two-plotting-aesthetics-questions-tgraph-and-taxis/61900/5 "2024-10-12T05:02:17Z")

</div>

For the function line issue, it’s because your functions are defined from 9.5 (or 10.5 as you would like) to 200 (even though you are only plotting up to 37), and by default ROOT draws TF1 by sampling 100 points in x, so there aren’t enough points; you can change that number with TF1::SetNpx:  
[https://root.cern/doc/master/classTF1.html#a6a04facaa58306988a3189b4e8713aa5](https://root.cern/doc/master/classTF1.html#a6a04facaa58306988a3189b4e8713aa5)  
For your case, you could increase that npx (around 500 seems to look ok), or reduce the upper limit of your functions (e.g. to 40), or a combination of both. Using DrawFrame as @couet suggested, and these 2 options:

```auto
  TCanvas *c1 = new TCanvas("c1","",700,500);

  gStyle->SetOptStat(0);
  gStyle->SetLegendBorderSize(0);

  c1->DrawFrame(0,0,37,1.30);

  TGaxis *axis = new TGaxis(37,0.0, 37, 1.15,0,1.5,51,"+L");
  axis->SetNdivisions(505);
  axis->SetLabelFont(gStyle->GetLabelFont("Y"));
  axis->SetTitleFont(gStyle->GetTitleFont("Y"));
  axis->SetTitle(" Background Rate [MHz]");
  axis->CenterTitle();
  axis->SetTitleSize(0.08);
  axis->SetTitleOffset(0.5);
  axis->Draw();

  TLine *line = new TLine(10.5,0,10.5,1.30);
  line->SetLineStyle(2);
  line->Draw();

  double Act_Zr88_0 = 1.11; // GBq
  double tHalf_Zr88 = 83.4; // days
  double tHalf_Y88 = 106.629; // days
  double Eff_Zr88 = 0.0002;
  double Eff_Y88 = 0.0035;

  // BLUE, increasing Npx
  TF1 *fBkg_Zr88 = new TF1("fBkg_Zr88","1000*[3]*[0]*exp(-log(2.0)*x/[1])",10.5,200);
  fBkg_Zr88->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88, Eff_Zr88);
  fBkg_Zr88->SetLineColor(kBlue+2);
  fBkg_Zr88->SetLineWidth(4);
  fBkg_Zr88->SetLineStyle(7);
  fBkg_Zr88->SetNpx(500);
  fBkg_Zr88->Draw("same");

  // RED, reducing range of TF1
  TF1 *fBkg_Y88 = new TF1("fBkg_Y88","1000*[4]*(1.0/[1])/(1.0/[2]-1.0/[1])*[0]*(exp(-log(2.0)*x/[1])-exp(-log(2.0)*x/[2]))",10.5,40);
  fBkg_Y88->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88, Eff_Zr88, Eff_Y88);
  fBkg_Y88->SetLineColor(kRed+2);
  fBkg_Y88->SetLineWidth(4);
  fBkg_Y88->SetLineStyle(7);
  fBkg_Y88->Draw("same");

```

 ![c1](https://root-forum.cern.ch/uploads/default/original/3X/6/b/6bc99d675b7f15e7c7d42ebb69b6a3a02941a8b7.png)

---

<div class="post-metadata">

**Author:** ![Will\_Flanagan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/will_flanagan/32/35050_2.png) [@Will\_Flanagan](https://root-forum.cern.ch/u/Will_Flanagan)\
**Post date:** [October 13, 2024, 3:25am UTC](https://root-forum.cern.ch/t/two-plotting-aesthetics-questions-tgraph-and-taxis/61900/6 "2024-10-13T03:25:06Z")

</div>

@dastudillo, your answers have been quite informative to me on quite a few facets of this plotting macro from DrawFrame to SetNpx. Thank you for your time!
