Hello,
when i use SetBinLabel, my TH1F starts to ignore the SetNdivisions… what can I do in order to get the ticks drawn as i want them? (I know, I could draw them with Line, but there should be a way, to draw ticks even with BinLabels…)
When you define yourself the bin labels only the main tick marks are drawn. One per bin. SetNdivisions has no effect. Try to open the graphics editor and select the X axis. then change the divisions. You will see that it does not affect the display. Only the main ticks are drawn.
I have the same problem and I would like to know if this is still true. In this case, I want to have only primary divisions shown every 9 bins, then I want to change the label from multiples of 9 to something more meaningful (still integers however, although I don’t know any other way than SetBinLabel to change that, which accepts only char input for labels). Here’s a simple example illustrating what I want to do.
{
stringstream bin;
int gliid;
TH1D *GLImax_h = new TH1D("Maximum displacement per GLI","Maximum displacement per GLI",144,0,144);
for (int m=0;m<16;++m) {
gliid=100*(m/8+1)+(m%8)*10+1;
for (int j=0;j<9;++j) {
GLImax_h->Fill(9*m+j,j);
if (j==0) {
bin.str("");
bin << gliid;
GLImax_h->GetXaxis()->SetBinLabel(9*m+j+1,bin.str().c_str());
}
++gliid;
}
}
GLImax_h->SetName("GLI max");
GLImax_h->GetYaxis()->SetTitle("Absolute Displacement (#mum)");
GLImax_h->GetXaxis()->SetTitle("GLI Id");
GLImax_h->GetXaxis()->SetNdivisions(-16);
GLImax_h->Draw("hist");
}
When the SetBinLabel is commented out, the SetNdivisions call works like a charm. As soon as I add the SetBinLabel, I get as you say, one division per bin. Is there any workaround? I am using ROOT version 5.30.
One way I see would be to draw 2 axis. one with the labels and no ticks,
And a 2nd one on top with the tick you want and no labels.
Or may be be changing the number of axis divisions . Have you tried it ?
[quote=“couet”]One way I see would be to draw 2 axis. one with the labels and no ticks,
And a 2nd one on top with the tick you want and no labels. [/quote]
Ah, that could work indeed (no I haven’t tried yet). Simple question though, how do I make the first one (labels, no ticks), since when I set the Labels, I lose all control on the ticks? I tried to do the following without any success:
{
stringstream bin;
int gliid;
TH1D *GLImax_h = new TH1D("Maximum displacement per GLI","Maximum displacement per GLI",144,0,144);
for (int m=0;m<16;++m) {
gliid=100*(m/8+1)+(m%8)*10+1;
for (int j=0;j<9;++j) {
GLImax_h->Fill(9*m+j,j);
if (j==0) {
bin.str("");
bin << gliid;
GLImax_h->GetXaxis()->SetBinLabel(9*m+j+1,bin.str().c_str());
}
++gliid;
}
}
GLImax_h->SetName("GLI max");
GLImax_h->GetYaxis()->SetTitle("Absolute Displacement (#mum)");
GLImax_h->GetXaxis()->SetTitle("GLI Id");
GLImax_h->GetXaxis()->SetNdivisions(0);
GLImax_h->Draw("hist");
}
Unless you meant to add two axes on top of a blank slate (no labels, no ticks on the histo axis)?
[quote=“couet”]
Or may be be changing the number of axis divisions . Have you tried it ?[/quote]
Sorry, I’m confused again. Changing the number of divisions is what I tried originally… Can you explain in more details what you mean by this?
So I tried the said solution, and there is only one remaining problem: TGaxis doesn’t treat the Ndivisions properly. Code below:
{
stringstream bin;
int gliid;
TH1D *GLImax_h = new TH1D("Maximum displacement per GLI","Maximum displacement per GLI",144,0,144);
TF1 *f1=new TF1("f1","x",0,144);
TGaxis *labels = new TGaxis(0,0,144,0,"f1",16);
for (int m=0;m<16;++m) {
gliid=100*(m/8+1)+(m%8)*10+1;
for (int j=0;j<9;++j) {
GLImax_h->Fill(9*m+j,j);
if (j==0) {
bin.str("");
bin << gliid;
GLImax_h->GetXaxis()->SetBinLabel(9*m+j+1,bin.str().c_str());
}
++gliid;
}
}
GLImax_h->SetName("GLI max");
GLImax_h->GetYaxis()->SetTitle("Absolute Displacement (#mum)");
GLImax_h->GetXaxis()->SetTitle("GLI Id");
GLImax_h->SetTickLength(0,"X");
GLImax_h->Draw("hist");
labels->SetNdivisions(16); //Reiterate previous request in TGaxis declaration
labels->SetLabelSize(0);
labels->Draw();
With this, I get the labels right without tickmarks, but the TGaxis has fixed Ndivisions (14, one every 10 bins). I tried to play with the example provided in root.cern.ch/root/html/TGaxis.html#TGaxis:TGaxis%2 to see how tickmarks worked for TGaxis, and it seems that even in the example, ndiv=16 is not behaving as I want (i.e. one tickmark every 9 bins). Is there something fundamentally wrong about this number? Any further clues?