Draw TLine *under* the histogram

Hi all,

I would like to draw a TLine under the histogram, not over the histogram.

So far my workaround is to plot the histogram twice…
Is there a more convinient way to do that?

// foxwise.cpp

void foxwise(){
    TCanvas* canvas = new TCanvas();
    TH1F* h = new TH1F("h", "", 100, -10, 10);
    h->FillRandom("gaus", 100000);
    h->SetLineWidth(3);
    TLine* l = new TLine(-5, 0, 5, h->GetMaximum());
    l->SetLineColor(4);
    l->SetLineWidth(8);
    l->SetLineStyle(9);

    //works but line is OVER the histogram
    // h->Draw();
    // l->Draw();

    // line isn't drawn
    // l->Draw();
    // h->Draw();

    // empty canvas
    // l->Draw();
    // h->Draw("same");

    // my workaround for now...
    h->Draw();
    l->Draw();
    h->Draw("same");

    canvas->Update();
}

To be drawn TLine needs a coordinates space. One way to define the coordinates spaces is simply to draw the histogram. In that sense your approach is correct. If you want to avoid drawing twice the same histogram you can first define the space with DrawFrame. Your macro becomes:

void foxwise2(){
    TCanvas* canvas = new TCanvas();
    TH1F* h = new TH1F("h", "", 100, -10, 10);
    h->FillRandom("gaus", 100000);
    h->SetFillColor(2);
    h->SetLineWidth(3);
    TLine* l = new TLine(-5, 0, 5, h->GetMaximum());
    l->SetLineColor(4);
    l->SetLineWidth(8);
    l->SetLineStyle(9);

    canvas->DrawFrame(h->GetXaxis()->GetXmin(),h->GetYaxis()->GetXmin(),
                      h->GetXaxis()->GetXmax(),1.03*h->GetMaximum());
    l->Draw();
    h->Draw("same");
    canvas->RedrawAxis();
}

1 Like

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