# Problem with changing axis label

**URL:** <https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958>\
**Category:** Newbie\
**Created:** [May 12, 2022, 1:26pm UTC](https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958 "2022-05-12T13:26:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzriu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@mzriu](https://root-forum.cern.ch/u/mzriu)\
**Post date:** [May 12, 2022, 1:26pm UTC](https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958/1 "2022-05-12T13:26:17Z")

</div>

Hello everyone,

I am trying to plot a graph with certain y values with respect to their run numbers along the x-axis. Here is my code :

```auto
        const Int_t n = 11;
        Double_t run[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; //run index that needs to be changed on the x-axis

/* Some code to compute my y values */

       TCanvas *c1 = new TCanvas("c1", "c1", 700, 500);
	c1->SetGrid();
	
	TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);
	
	gr->GetXaxis()->ChangeLabel(1, -1, -1, -1, -1, -1, "0");
	gr->GetXaxis()->ChangeLabel(2, -1, -1, -1, -1, -1, "1");
	gr->GetXaxis()->ChangeLabel(3, -1, -1, -1, -1, -1, "2");
	gr->GetXaxis()->ChangeLabel(4, -1, -1, -1, -1, -1, "139");
	gr->GetXaxis()->ChangeLabel(5, -1, -1, -1, -1, -1, "140");
	gr->GetXaxis()->ChangeLabel(6, -1, -1, -1, -1, -1, "141");
	gr->GetXaxis()->ChangeLabel(7, -1, -1, -1, -1, -1, "142");
	gr->GetXaxis()->ChangeLabel(8, -1, -1, -1, -1, -1, "144");
	gr->GetXaxis()->ChangeLabel(9, -1, -1, -1, -1, -1, "147");
	gr->GetXaxis()->ChangeLabel(10, -1, -1, -1, -1, -1, "149");
	gr->GetXaxis()->ChangeLabel(11, -1, -1, -1, -1, -1, "150");
	
	gr->SetTitle("#phi_{477}/#phi_{980}");
	gr->GetXaxis()->SetLimits(0.65, 11.45);
	gr->SetMinimum(0.1);
	gr->SetMaximum(2.5);
	gr->GetXaxis()->SetTitle("Run index");
	gr->GetYaxis()->SetTitle("#phi_{477}/#phi_{980}");
	
	
	
	gr->SetMarkerStyle(20);
	gr->SetMarkerColor(kBlue+1);
	gr->SetMarkerSize(0.9);
	gr->Fit("fun_fit");
	gr->Draw("AP");

```

As you can see I have different labels for my 11 runs going from 0 to 150. Using this type of code worked for me before but now it just won’t make it as shown in the next picture.  
[c1.pdf](https://root-forum.cern.ch/uploads/short-url/lU63eJdSyyVe7bebMvgf1GK22Ji.pdf) (14.4 KB)

Do you have any idea what’s wrong ?

---

<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 12, 2022, 1:40pm UTC](https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958/2 "2022-05-12T13:40:21Z")

</div>

can you provide a running example ?

```auto
root [0] 
Processing mz.C...
/Users/couet/roottest/./mz.C:11:48: error: use of class template 'ratio' requires template arguments
   TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);
                                               ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ratio:248:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS ratio
                           ^
/Users/couet/roottest/./mz.C:11:58: error: use of undeclared identifier 'sig_ratio'
   TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);
                                                         ^

```

---

<div class="post-metadata">

**Author:** ![mzriu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@mzriu](https://root-forum.cern.ch/u/mzriu)\
**Post date:** [May 12, 2022, 1:43pm UTC](https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958/3 "2022-05-12T13:43:37Z")

</div>

```auto
//Calcul de l'incertitude sur le rapport d'embranchement du gamma à 477keV

#include "TH1.h"
#include "TMath.h"
#include "TF1.h"
#include "TLegend.h"
#include "TCanvas.h"

{

	const Int_t n = 11;
	
	Double_t run[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
	//RUN 0, 1, 2, 139, 140, 141, 142, 144, 147, 149, 150
	
	//ANCIENNE ANALYSE AVEC LES VIEILLES EFFICACITES
	//Double_t phi_477[n] = {212574, 254687, 259500, 720549, 701025, 658536, 589420, 536485, 522301, 587538, 577487};
	//Double_t sig_phi_477[n] = {24142, 34929, 36492, 80856, 82734, 75177, 68634, 82093, 66930, 70391, 67724};
	
	
	//NOUVELLES EFFICACITES MAIS SIGNAL A MU +- 3SIG
	//Double_t phi_477[n] = {216175, 259001, 263895, 732620, 712769, 669568, 599294, 545473, 531051, 597381, 587162};
	//Double_t sig_phi_477[n] = {5442, 20953, 22871, 22335, 33928, 24775, 26098, 59134, 36649, 30988, 26844};
	
	//SIGNAL ENTRE BORNES DEFINIES AVEC LES CHAINES
	Double_t phi_477[n] = {218332, 264278, 267550, 708438, 691411, 657000, 585347, 522908, 515426, 576696, 571899};
	Double_t sig_phi_477[n] = {5314, 19598, 20671, 20553, 29995, 22583, 23156, 49936, 32215, 27198, 23759};
	
	
	//Double_t phi_980[n] = {140049, 181945, 174288, 459274, 462182, 455030, 391348, 337135, 341731, 386646, 384988};
	//Double_t sig_phi_980[n] = {19583, 25457, 24388, 62772, 63122, 62124, 53429, 46046, 46658, 52792, 52564};
	
	Double_t phi_980[n] = {162792, 195363, 186831, 497068, 494028, 486971, 407969, 348544, 357062, 408052, 406690};
	Double_t sig_phi_980[n] = {1298, 1810, 1775, 4248, 4454, 4063, 3399, 3180, 3012, 3469, 3438};

	Double_t ratio[n];
	Double_t sig_ratio[n];
	
	for(Int_t i = 0; i < n; i++){
		ratio[i] = phi_477[i]/phi_980[i];
		sig_ratio[i] = pow(pow(sig_phi_477[i]/phi_980[i], 2) + pow(phi_477[i]*sig_phi_980[i]/pow(phi_980[i], 2), 2), 0.5);
		cout << "rat = " << ratio[i] << endl;
		cout << "sig_rat = " << sig_ratio[i] << endl;
		}
		
	TF1 *fun_fit = new TF1("fun_fit", "[0]", 1, 11);
	
	TCanvas *c1 = new TCanvas("c1", "c1", 700, 500);
	c1->SetGrid();
	
	TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);
	
	gr->GetXaxis()->ChangeLabel(1, -1, -1, -1, -1, -1, "0");
	gr->GetXaxis()->ChangeLabel(2, -1, -1, -1, -1, -1, "1");
	gr->GetXaxis()->ChangeLabel(3, -1, -1, -1, -1, -1, "2");
	gr->GetXaxis()->ChangeLabel(4, -1, -1, -1, -1, -1, "139");
	gr->GetXaxis()->ChangeLabel(5, -1, -1, -1, -1, -1, "140");
	gr->GetXaxis()->ChangeLabel(6, -1, -1, -1, -1, -1, "141");
	gr->GetXaxis()->ChangeLabel(7, -1, -1, -1, -1, -1, "142");
	gr->GetXaxis()->ChangeLabel(8, -1, -1, -1, -1, -1, "144");
	gr->GetXaxis()->ChangeLabel(9, -1, -1, -1, -1, -1, "147");
	gr->GetXaxis()->ChangeLabel(10, -1, -1, -1, -1, -1, "149");
	gr->GetXaxis()->ChangeLabel(11, -1, -1, -1, -1, -1, "150");
	
	gr->SetTitle("#phi_{477}/#phi_{980}");
	gr->GetXaxis()->SetLimits(0.65, 11.45);
	gr->SetMinimum(0.1);
	gr->SetMaximum(2.5);
	gr->GetXaxis()->SetTitle("Run index");
	gr->GetYaxis()->SetTitle("#phi_{477}/#phi_{980}");
	
	
	
	gr->SetMarkerStyle(20);
	gr->SetMarkerColor(kBlue+1);
	gr->SetMarkerSize(0.9);
	gr->Fit("fun_fit");
	gr->Draw("AP");
	
	

}

```

---

<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 12, 2022, 1:49pm UTC](https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958/4 "2022-05-12T13:49:44Z")

</div>

```auto
//Calcul de l'incertitude sur le rapport d'embranchement du gamma à 477keV

#include "TH1.h"
#include "TMath.h"
#include "TF1.h"
#include "TLegend.h"
#include "TCanvas.h"

{

   const Int_t n = 11;

   Double_t run[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
   //RUN 0, 1, 2, 139, 140, 141, 142, 144, 147, 149, 150

   //ANCIENNE ANALYSE AVEC LES VIEILLES EFFICACITES
   //Double_t phi_477[n] = {212574, 254687, 259500, 720549, 701025, 658536, 589420, 536485, 522301, 587538, 577487};
   //Double_t sig_phi_477[n] = {24142, 34929, 36492, 80856, 82734, 75177, 68634, 82093, 66930, 70391, 67724};

   //NOUVELLES EFFICACITES MAIS SIGNAL A MU +- 3SIG
   //Double_t phi_477[n] = {216175, 259001, 263895, 732620, 712769, 669568, 599294, 545473, 531051, 597381, 587162};
   //Double_t sig_phi_477[n] = {5442, 20953, 22871, 22335, 33928, 24775, 26098, 59134, 36649, 30988, 26844};

   //SIGNAL ENTRE BORNES DEFINIES AVEC LES CHAINES
   Double_t phi_477[n] = {218332, 264278, 267550, 708438, 691411, 657000, 585347, 522908, 515426, 576696, 571899};
   Double_t sig_phi_477[n] = {5314, 19598, 20671, 20553, 29995, 22583, 23156, 49936, 32215, 27198, 23759};

   //Double_t phi_980[n] = {140049, 181945, 174288, 459274, 462182, 455030, 391348, 337135, 341731, 386646, 384988};
   //Double_t sig_phi_980[n] = {19583, 25457, 24388, 62772, 63122, 62124, 53429, 46046, 46658, 52792, 52564};

   Double_t phi_980[n] = {162792, 195363, 186831, 497068, 494028, 486971, 407969, 348544, 357062, 408052, 406690};
   Double_t sig_phi_980[n] = {1298, 1810, 1775, 4248, 4454, 4063, 3399, 3180, 3012, 3469, 3438};

   Double_t ratio[n];
   Double_t sig_ratio[n];

   for(Int_t i = 0; i < n; i++){
      ratio[i] = phi_477[i]/phi_980[i];
      sig_ratio[i] = pow(pow(sig_phi_477[i]/phi_980[i], 2) + pow(phi_477[i]*sig_phi_980[i]/pow(phi_980[i], 2), 2), 0.5);
      cout << "rat = " << ratio[i] << endl;
      cout << "sig_rat = " << sig_ratio[i] << endl;
      }

   TF1 *fun_fit = new TF1("fun_fit", "[0]", 1, 11);

   TCanvas *c1 = new TCanvas("c1", "c1", 700, 500);
   c1->SetGrid();

   TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);

   gr->SetTitle("#phi_{477}/#phi_{980}");
   gr->GetXaxis()->SetLimits(0.65, 11.45);
   gr->SetMinimum(0.1);
   gr->SetMaximum(2.5);
   gr->GetXaxis()->SetTitle("Run index");
   gr->GetYaxis()->SetTitle("#phi_{477}/#phi_{980}");
   gr->GetXaxis()->SetNdivisions(515);

   gr->GetXaxis()->ChangeLabel(1, -1, -1, -1, -1, -1, "0");
   gr->GetXaxis()->ChangeLabel(2, -1, -1, -1, -1, -1, "1");
   gr->GetXaxis()->ChangeLabel(3, -1, -1, -1, -1, -1, "2");
   gr->GetXaxis()->ChangeLabel(4, -1, -1, -1, -1, -1, "139");
   gr->GetXaxis()->ChangeLabel(5, -1, -1, -1, -1, -1, "140");
   gr->GetXaxis()->ChangeLabel(6, -1, -1, -1, -1, -1, "141");
   gr->GetXaxis()->ChangeLabel(7, -1, -1, -1, -1, -1, "142");
   gr->GetXaxis()->ChangeLabel(8, -1, -1, -1, -1, -1, "144");
   gr->GetXaxis()->ChangeLabel(9, -1, -1, -1, -1, -1, "147");
   gr->GetXaxis()->ChangeLabel(10, -1, -1, -1, -1, -1, "149");
   gr->GetXaxis()->ChangeLabel(11, -1, -1, -1, -1, -1, "150");

   gr->SetMarkerStyle(20);
   gr->SetMarkerColor(kBlue+1);
   gr->SetMarkerSize(0.9);
   gr->Fit("fun_fit");
   gr->Draw("AP");
}

```

 ![Screenshot 2022-05-12 at 15.48.40](https://root-forum.cern.ch/uploads/default/original/3X/8/b/8bf0342668b757dd33578b6755c2c682c06c79e7.png)

---

<div class="post-metadata">

**Author:** ![mzriu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@mzriu](https://root-forum.cern.ch/u/mzriu)\
**Post date:** [May 12, 2022, 1:56pm UTC](https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958/5 "2022-05-12T13:56:23Z")

</div>

> [@couet](#):
>
> `gr->GetXaxis()->SetNdivisions(515);`

Okay it works but I have to check why

Thanks for the answer though !

Best

---

<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 12, 2022, 1:58pm UTC](https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958/6 "2022-05-12T13:58:45Z")

</div>

`ChangeLabel` change existing labels. In you case the label optimization displayed to low number of labels.

---

<div class="post-metadata">

**Author:** ![mzriu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@mzriu](https://root-forum.cern.ch/u/mzriu)\
**Post date:** [May 12, 2022, 2:03pm UTC](https://root-forum.cern.ch/t/problem-with-changing-axis-label/49958/7 "2022-05-12T14:03:42Z")

</div>

Oh I see, for my other code where it works I actually have 9 and not 11 points to plot so the default value of n=510 works there but not here !

Thanks for your reply !  
Best
