Changing labels in graph

Dear all I would like to change the labels on the X axis of my graph, I have already a working code (derived from a similar topic in the root forum) how as you can see in the plot the point on the graph mismatch the ranges on the X axis (for instance I would like to have the first point of the graph to be in the middle of [72,112] range and so one), and the first and last point don’t touch the verticale lines
Could someone explain how to fix this please ?
I the code is here.


#include "TROOT.h"    
#include "TObject.h" 
#include "TFile.h"
#include "TTree.h"
#include "TH2.h"
#include "TMath.h"
#include "TGraph.h"
#include "TColor.h"
#include "TCanvas.h"
#include "TLegend.h"
#include <TStyle.h>
#include <iostream>

#include "AtlasStyle.h"

#include "TROOT.h"



void test()
  
{
 
   TCanvas *c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);
const Int_t n = 10;
  char const *range[n] = {"[72-112]","[74-110]","[76-108]","[78-106]","[80-104]","[82-102]","[84-100]","[86-100]","[88-98]","[92-94]"};
   int i;
 Float_t  x[n]  = {1,2,3,4,5,6,7,8,9,10}; 
 Float_t  y[n]  = {114.9,113.7,115.4,112.1,113.5,113.8,111.8,112.2, 111.9, 110.7};
 TH1F *h = new TH1F("h","",n,x[0],x[n-1]);
 h->SetTitle("");
 h->GetYaxis()->SetTitleOffset(1.);
 h->GetXaxis()->SetTitleOffset(1.);
 h->GetYaxis()->SetTitle("sig");
 h->GetXaxis()->SetTitle("range [GeV]");
 for (i=1;i<=n;i++) h->GetXaxis()->SetBinLabel(i,range[i-1]);
 h->SetMaximum(200);
 h->SetStats(0);
 h->Draw("");
 TGraph *gr = new TGraph (n, x, y);
 gr->SetMarkerColor(4);
 gr->SetMarkerStyle(20);
 gr->Draw("CP");
}


Thanks in advance,
Best Regards,

Diallo.

{

   TCanvas *c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);
   c1->SetGridx();
   const Int_t n = 10;
   char const *range[n] = {"[72-112]","[74-110]","[76-108]","[78-106]","[80-104]","[82-102]","[84-100]","[86-100]","[88-98]","[92-94]"};
   int i;
   Float_t  x[n]  = {1,2,3,4,5,6,7,8,9,10};
   Float_t  y[n]  = {114.9,113.7,115.4,112.1,113.5,113.8,111.8,112.2, 111.9, 110.7};
   TH1F *h = new TH1F("h","",n,0.5,10.5);
   h->SetTitle("");
   h->GetYaxis()->SetTitleOffset(1.);
   h->GetXaxis()->SetTitleOffset(1.);
   h->GetYaxis()->SetTitle("sig");
   h->GetXaxis()->SetTitle("range [GeV]");
   h->GetXaxis()->SetNdivisions(-10);
   for (i=1;i<=n;i++) h->GetXaxis()->SetBinLabel(i,range[i-1]);
   h->SetMaximum(200);
   h->SetStats(0);
   h->Draw("");
   TGraph *gr = new TGraph (n, x, y);
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(20);
   gr->Draw("CP");
}

1 Like

Hi Diallo,
change your hist ctor to:

TH1F *h = new TH1F("h","",n,x[0]-0.5,x[n-1]+0.5);

that should do it.

Cheers
Otto

Dear Couet,

Thanks too much, I really appreciate it :slight_smile:
Best Regards,

Diallo.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.