I’m trying to change the Th1’s Y axis’s labels, but when I’m trying to do it, I found the number of Y axis bin is 1. And I can not change the Y axis labels by SetBinLabel(). What should I do?
I try to draw a TH1F object with option (“hbar”), but the fit result won’t show. How to show it?
The default of (“hbar”) is using the left Y axis as X axis, but I want to use the right Y axis as X axis, how to do that?
Could someone help me, plesa? Thanks a lot~
couet
March 4, 2008, 9:18am
2
There is an example showing how to use the bar and hbar options here:
$ROOTSYS/tutorials/hist/hbars.C
Thanks, but this tutrial doesn’t help.
I want to do the following things:
show the fit curve while using the “hbar” option
show the bars using right Y axis, not left Y axis.
change the TH1’s Y axis’ label.
The tutrial is simple and I still have no idea how to do these.
couet
March 5, 2008, 11:02am
4
show the fit curve while using the “hbar” option
This is not implemented. Fit curves cannot not be shown horizontally.
couet
March 5, 2008, 1:06pm
5
{
h = new TH1F("h1","h1",10,-3,3);
h->FillRandom("gaus",10000);
h->Fit("gaus");
TF1 *g=h->GetListOfFunctions()->FindObject("gaus");
Double_t x1 = g->GetXmin();
Double_t x2 = g->GetXmax();
Double_t dx = (x2-x1)/100;
Double_t x[100], y[100];
x[0] = x1;
y[0] = g->Eval(x[0]);
for (int i=1; i<100; i++) {
x1 = x1+dx;
x[i] = x1;
y[i] = g->Eval(x[i]);
}
h->Draw("hbar2");
TGraph *gr = new TGraph(100,y,x);
gr->Draw("L");
}
Great Idea! Use TGraph to draw the fit result!! It works!
Thanks a lot!
Then how to change the Y axis label? can you help me?
I try:
h->GetYaxis()->SetBinLabel(1,“Tom”); // this one works
h->GetYaxis()->SetBinLabel(2,"Mary"); // this doesn't work
->Error in TAxis::SetBinLabel : Illegal bin number: 2
couet
March 6, 2008, 8:50am
7
Here is an example:
{
h = new TH1F("h1","h1",10,-3,3);
h->FillRandom("gaus",10000);
h->Fit("gaus");
for (int bin=1; bin<=10; bin++) {
h->GetXaxis()->SetBinLabel(bin,Form("Bin #%d",bin));
}
TF1 *g=h->GetListOfFunctions()->FindObject("gaus");
Double_t x1 = g->GetXmin();
Double_t x2 = g->GetXmax();
Double_t dx = (x2-x1)/100;
Double_t x[100], y[100];
x[0] = x1;
y[0] = g->Eval(x[0]);
for (int i=1; i<100; i++) {
x1 = x1+dx;
x[i] = x1;
y[i] = g->Eval(x[i]);
}
h->Draw("hbar2");
TGraph *gr = new TGraph(100,y,x);
gr->Draw("L");
}
Thanks for your help, you have changed the X axis labels, but not Y axis.
If you change
h->GetXaxis()->SetBinLabel(bin,Form(“Bin #%d”,bin));
to
h->GetYaxis()->SetBinLabel(bin,Form(“Bin #%d”,bin));
it shows:
Error in TAxis::SetBinLabel : Illegal bin number: 2
Error in TAxis::SetBinLabel : Illegal bin number: 3
Error in TAxis::SetBinLabel : Illegal bin number: 4
Error in TAxis::SetBinLabel : Illegal bin number: 5
Error in TAxis::SetBinLabel : Illegal bin number: 6
Error in TAxis::SetBinLabel : Illegal bin number: 7
Error in TAxis::SetBinLabel : Illegal bin number: 8
Error in TAxis::SetBinLabel : Illegal bin number: 9
Error in TAxis::SetBinLabel : Illegal bin number: 10
I want to change the Y axis’s label ( with “hbar” option, it’s X axis now)
couet
March 7, 2008, 9:32am
9
That’s because for a 1D histogram the Y axis has no bins of course.
try this:
{
h = new TH1F("h1","h1",10,-3,3);
h->FillRandom("gaus",10000);
h->Fit("gaus");
for (int bin=1; bin<=10; bin++) {
h->GetXaxis()->SetBinLabel(bin,Form("Bin #%d",bin));
}
TF1 *g=h->GetListOfFunctions()->FindObject("gaus");
Double_t x1 = g->GetXmin();
Double_t x2 = g->GetXmax();
Double_t dx = (x2-x1)/100;
Double_t x[100], y[100];
x[0] = x1;
y[0] = g->Eval(x[0]);
for (int i=1; i<100; i++) {
x1 = x1+dx;
x[i] = x1;
y[i] = g->Eval(x[i]);
}
h->GetYaxis()->Set(5,h->GetMinimum(),h->GetMaximum());
h->GetYaxis()->SetBinLabel(1,"aaa");
h->GetYaxis()->SetBinLabel(2,"bbb");
h->GetYaxis()->SetBinLabel(3,"bbb");
h->GetYaxis()->SetBinLabel(4,"bbb");
h->GetYaxis()->SetBinLabel(5,"ccc");
h->Draw("hbar2");
TGraph *gr = new TGraph(100,y,x);
gr->Draw("L");
}