THStack axis labels

Hi all,
I’m using pyROOT, and I can’t get any axis labels to appear on my THStack’s. I thought I remember hearing that the THStack would display the same axis labels that the first histogram added has, but that is not happening for me. For example:

c1 = TCanvas("default","default") first = TH1F("first","first",10,0,10) second = TH1F("second","second",10,0,10) for i in range(100) : if i < 33 : first.Fill(i % 2) second.Fill(i % 4) elif i < 66 : first.Fill(i % 5) second.Fill(i % 7) else : first.Fill(i % 8) second.Fill(i % 10) stack = THStack("stack","stack") stack.Add(first) stack.Add(second) first.GetXaxis().SetTitle("counts") second.GetXaxis().SetTitle("counts") second.SetLineColor(kRed) stack.Draw() c1.Print("stack.png")

The result does not have any label on the x axis. I’m using ROOT v5.16/00, so maybe this is solved in newer versions? Thanks for any help.

when drawing with the default option “stack”, you draw the cumulated sum of the input histograms, not the histograms.
To achieve what you want simply do (here in C++)

stack->Draw(); stack->GetYaxis()->SetTitle("my title"); gPad-<Modified();
Rene

Sorry for resuming this old topic…
My problem is that I can’t save the axis labels.
Example: in one ROOT interactive session:

TFile FStack("StackTest.root", "RECREATE"); TCanvas* pCanvas = new TCanvas("CGaussStack", "Gaussian stacks") pCanvas->cd() THStack* pHStack = new THStack("HGaussStack", "Gaussian stacks"); TH1F* pHist = 0; pHist = new TH1F("HGauss1", "gaussian (1)", 100, -5., 5.); pHist->FillRandom("gaus", 1000); pHist->SetFillColor(2); pHStack->Add(pHist); pHist = new TH1F("HGauss2", "gaussian (2)", 100, -5., 5.); pHist->FillRandom("gaus", 500); pHist->SetFillColor(3); pHStack->Add(pHist); pHStack->Draw(); pHStack->GetXaxis()->SetTitle("z"); pCanvas->Modified(); pHStack->Write(); pCanvas->Write();
We see the label “z” on x axis. In a new ROOT interactive session:

TFile FStack("StackTest.root", "READ"); THStack* pHStack = (THStack*) FStack.Get("HGaussStack"); pHStack->Draw(); TCanvas* pCanvas = (TCanvas*) FStack.Get("CGaussStack"); pCanvas->Draw();
No x axis label anymore, neither saving the histogram stack nor the whole canvas.
I’m using some sort of ROOT 5.18/00.

Did this ever get resolved? I am seeing the same problem with THStack, which is a shame as it is exactly what I’m looking for.

                        hs->Draw(" ");
                        hs->GetXaxis()->SetTitle(Vars[v]+" ("+Units[v]+" )");
                        hs->GetYaxis()->SetTitle("Candidates");

Doesn’t work, it writes out an empty canvas (with axes, but no labels or data) in root 5.18 and 5.20.

Conor

Add

canvas->Update(); after hs->Draw(…");

It is important that you read the doc to understand why this call is important.

Rene

Sorry, Rene, I had already tried this call without luck. I have tried calling Update, Draw, etc in various combinations. In all cases, I get the stacked histogram disappearing from the canvas, and the axes showing up.

    for(UInt_t page =0; page<pages; page++){
                cout << "page = " << page <<endl;
                outFile->cd();
                char *canvName = new char[15];
                sprintf(canvName,"Plots page %i",page);
                TCanvas *c = new TCanvas(canvName,canvName, CanvWidth,CanvHeight);
                c->SetFillColor(0);
                c->Divide(PlotCols,PlotRows);
                for(UInt_t v = page*ppp; v<(page*ppp)+ppp; v++){
                        c->cd(v%ppp +1);
                        THStack *hs = new THStack(Vars[v],Vars[v]);
                        TLegend *legend = new TLegend(0.6,0.7,0.89,0.89);
                        cout << "pad = " << v -page*ppp << endl;
                        for(UInt_t f=0; f<nFiles; f++){
                                TH1F *Histo = new TH1F(Vars[v]+" "+LegendNames[f],Vars[v],VarBins[v],VarLower[v],VarUpper[v]);
                                Trees[f]->Draw(Vars[v]+">>"+Vars[v]+" "+LegendNames[f],Cuts[f]);
                                Histo->SetLineColor(Colors[f]);
                                Histo->SetFillColor(Colors[f]);
                                Histo->SetFillStyle(FillStyles[f]);
                                Histo->SetStats(false);
                                legend->AddEntry(Histo,LegendNames[f],"f");
                                hs->Add(Histo);
                        }
                        hs->Draw(" ");
                        c->Update();
                        hs->GetXaxis()->SetTitle(Vars[v]+" ("+Units[v]+" )");
                        hs->GetYaxis()->SetTitle("Candidates");

                        legend->SetShadowColor(0);
                        legend->SetLineColor(0);
                        legend->SetTextSize(0.05);
                        legend->SetFillColor(0);
                        legend->Draw();
                        //c->Update();

                }
                c->Write();
                delete c;
        }
        outFile->Write();
        outFile->Close();