Hello
I have TGraph with N data points and to each data point I’d like have a corresponding string as label on the X-axis. I tried to use this example:
{
#include <sstream>;
Int_t numchains=3;
Double_t chain[numchains];
Double_t chainerr[numchains];
for (Int_t i=0;i<numchains;i++){
chain[i]=i;
chainerr[i]=0.5;
};
Double_t time[numchains] = {
0.32528,
2.48328,
0.3505
};
Double_t l2timerms[numchains] = {
0.305687,
12.0619,
0.189249
};
std::string names[numchains] = {
"sequence_L2_muon_standalone_mu6l",
"sequence_L2_mu6l",
"sequence_L2_te650"
};
TGraphErrors *l2timevschain = new TGraphErrors(numchains,chain,time,chainerr,l2timerms);
for(Int_t k=0;k<numchains;k++){
l2timevschain->GetHistogram()->GetXaxis()->SetBinLabel(k,names[k].c_str());
};
l2timevschain->SetMarkerStyle(21);
l2timevschain->Draw("AP");
}
but it gives me a graph where the X-axis labels are all on the left edge of the graph and do not coincide with the data points. How can I fix this?
thanks
Ignacio
couet
August 29, 2008, 7:54am
2
The labels are set to the bins of the underlaying histogram. By default this histogram has 100 bins so if you set the labels to the first 3 bins you get what you described. I see two possible ways:
You set the labels to the bins numbers corresponding to the points positions.
You reduce the bins number of the underlaying histogram to 3. The following example shows that solution.
{
TCanvas *c1 = new TCanvas("c1", "c1",15,49,1051,500);
Int_t numchains=3;
Double_t chain[numchains];
Double_t chainerr[numchains];
for (Int_t i=0;i<numchains;i++){
chain[i]=i;
chainerr[i]=0.5;
};
Double_t time[numchains] = {
0.32528,
2.48328,
0.3505
};
Double_t l2timerms[numchains] = {
0.305687,
12.0619,
0.189249
};
std::string names[numchains] = {
"sequence_L2_muon_standalone_mu6l",
"sequence_L2_mu6l",
"sequence_L2_te650"
};
TGraphErrors *l2timevschain = new TGraphErrors(numchains,chain,time,chainerr,l2timerms);
TAxis *ax = l2timevschain->GetHistogram()->GetXaxis();
Double_t x1 = ax->GetBinLowEdge(1);
Double_t x2 = ax->GetBinUpEdge(ax->GetNbins());
l2timevschain->GetHistogram()->GetXaxis()->Set(3,x1,x2);
for(Int_t k=0;k<numchains;k++){
l2timevschain->GetHistogram()->GetXaxis()->SetBinLabel(k+1,names[k].c_str());
}
l2timevschain->SetMarkerStyle(21);
l2timevschain->Draw("AP");
}
1 Like