TGraphErrors with X-axis of “char” type

Hi, I would like to plot two TGraphErrors with type “char” on the X axis. I have tried writing this script below but it doesn’t work well. can someone help me

{
   auto c43 = new TCanvas("c43","c43",200,10,600,400);
   c43->DrawFrame(0., 0.2, 6., 1.6);
   double x[5]    = {1, 2, 3, 4,5};
   double zero[5] = {0, 0, 0, 0, 0};
 
   // Number collecte
   double py1[5]      = {30, 72, 12, 5, 67 };
   double ey_stat1[5] = {0.2, 0.18, 0.17, 0.2, 0.4};
   double ey_sys1[5]  = {0.5, 0.71, 0.76, 0.5, 0.45};
 
   // Energies
   double y2[5]       = {0.51, 0.79, 1.02, 1.14, 1.18};
   double ey_stat2[5] = {0.2, 0.18, 0.17, 0.2, 0.4};
   double ey_sys2[5]  = {0.63, 0.19, 0.7, 0.2, 0.7};
 
///////////////////////////////// 1 ///////////////////////////////////////
   auto graph1 = new TGraphErrors(5, x, py1, zero, ey_stat1);
   graph1->SetMarkerStyle(20);
   graph1->SetMarkerColor(2);
   graph1->SetFillColor(6);
   graph1->SetLineColor(2);
   graph1->Draw("Lp");
   
     gPad->Update();
   TAxis *X1 = graph1->GetXaxis();
   X1->SetNdivisions(9);
   X1->ChangeLabel(1,-1,-1,-1,-1,-1,"Alpha");
   X1->ChangeLabel(2,-1,-1,-1,-1,-1,"Beta");
   X1->ChangeLabel(3,-1,-1,-1,-1,-1,"Theta");
   X1->ChangeLabel(4,-1,-1,-1,-1,-1,"Gamma");
   X1->ChangeLabel(5,-1,-1,-1,-1,-1,"Phi");
   gPad->Modified();
   gPad->Update(); 
  
///////////////////////////////// 2 /////////////////////////////////////////
   auto graph2 = new TGraphErrors(5, x, y2, zero, ey_stat2);
   graph2->SetMarkerStyle(24);
   graph2->Draw("LP");

    gPad->Update();
   TAxis *X = graph2->GetXaxis();
   X->SetNdivisions(9);
   X->ChangeLabel(1,-1,-1,-1,-1,-1,"Alpha");
   X->ChangeLabel(2,-1,-1,-1,-1,-1,"Beta");
   X->ChangeLabel(3,-1,-1,-1,-1,-1,"Theta");
   X->ChangeLabel(4,-1,-1,-1,-1,-1,"Gamma");
   X->ChangeLabel(5,-1,-1,-1,-1,-1,"Phi");
   gPad->Modified();
   gPad->Update(); 
   
}


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi @Assalmi,

Could you please elaborate more on your issue regarding TGraphErrors, e.g. what is the problem that you are facing with the code you provided, etc.?

For what I can say at the moment, TGraphErrors always works on arrays of Double_t or Float_t, but I think we are still missing some information on what you are trying to achieve.

Cheers,
J.

Use the “A” (“axis”) option when drawing TGraphs (e.g. graph1->Draw("ALp");)

Thank you for your reply,
I would like to plot the error bars for each point weighing the parameters “number” and “energy” which corresponds to a characters : “alpha” “beta” “theta” …
so I would like to plot them as shown in the figure below but instead of the X-axis points 1, 2,3, 4,5 I would like to place the characters “alpha” “beta” “theta” …
I hope I explained my problem well
regrads
c43

Exactly that is correct.
Thank you so much
c43

Is it possible to rotate X-axis text (“Alpha” “beta” …) ? I cannot find an option corresponding to TAxis. if it is possible to rotate it as follows:
image
@couet

regards

Try the TAxis::LabelsOption method:

   // ...
   X1->SetBinLabel(X1->FindFixBin(5.), "Phi");
   X1->LabelsOption("v");
   X1->SetLabelSize(0.05);
   X1->SetLabelOffset(0.01);
   gPad->SetBottomMargin(0.15);
   gPad->Modified();
   // ...

Thank you for your help,
I tested the option: X1->LabelsOption("v"); but it doesn’t work ?

{
   auto c43 = new TCanvas("c43","c43",200,10,600,400);
   c43->DrawFrame(0., 0.2, 6., 1.6);
   double x[5]    = {1, 2, 3, 4,5};
   double zero[5] = {0, 0, 0, 0, 0};
 
   // Number collecte
   double py1[5]      = {1, 2, 4, 5, 9};
   double ey_stat1[5] = {0.2, 0.18, 0.17, 0.2, 0.4};
   double ey_sys1[5]  = {0.5, 0.71, 0.76, 0.5, 0.45};
 
   // Energies
   double y2[5]       = {0.51, 0.79, 1.02, 1.14, 1.18};
   double ey_stat2[5] = {0.2, 0.18, 0.17, 0.2, 0.4};
   double ey_sys2[5]  = {0.63, 0.19, 0.7, 0.2, 0.7};
 
///////////////////////////////// 1 ///////////////////////////////////////
   auto graph1 = new TGraphErrors(5, x, py1, zero, ey_stat1);
   graph1->GetYaxis()->SetRangeUser(0, 10);
   graph1->SetMarkerStyle(20);
   graph1->SetMarkerColor(2);
   graph1->SetFillColor(6);
   graph1->SetLineColor(2);
   
  
   TAxis *X1 = graph1->GetXaxis();
   X1->SetNdivisions(9);
   X1->LabelsOption("v");
   X1->ChangeLabel(1,-1,-1,-1,-1,-1,"Alpha");
   X1->ChangeLabel(2,-1,-1,-1,-1,-1,"Beta");
   X1->ChangeLabel(3,-1,-1,-1,-1,-1,"Theta");
   X1->ChangeLabel(4,-1,-1,-1,-1,-1,"Gamma");
   X1->ChangeLabel(5,-1,-1,-1,-1,-1,"Phi");
   gPad->Modified();
   gPad->Update(); 
 
   graph1->Draw("ALp");
///////////////////////////////// 2 /////////////////////////////////////////
   auto graph2 = new TGraphErrors(5, x, y2, zero, ey_stat2);
   graph2->SetMarkerStyle(24);
   graph2->Draw("LP same");

}

Use the “labAngle” and the “labAlign” parameters of the TAxis::ChangeLabel method (so, you can forget TAxis::LabelsOption ):

   // ...
   X1->ChangeLabel(5, 90., -1., 32, -1, -1, "Phi");
   X1->SetLabelOffset(-0.01);
   gPad->SetBottomMargin(0.15);
   gPad->Modified();
   // ...

Thank you very much for your help

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