Reposition Color Legend TH2 Lego Draw


How to move the color legend in TH2 lego like this:

to this:

inside the code?

Notice that “6” was overlapped with the color legend in the first figure. I know we can move it manually in canvas, but how to do it in code?

Also, “0” and “5” touch each other. How to give some space?



You can do it with


, here is the full example:

        TCanvas* c1 = new TCanvas("c1", "My awesome plot", 0, 0, 800, 600);
        c1->SetRightMargin(0.18); // to be able to see the z axis title
        TH2F *myHist = new TH2F("myHist", "My histogram;#Delta#phi;#Delta#eta;C(#Delta#eta, #Delta#phi)", 24, -6., +6., 20, -5., +5.);
        myHist->Fill(3., 3., 2500.);
        myHist->Fill(-3., -3., 10.);

        myHist->Draw("LEGO2 COLZ");

        gPad->Update(); // to be able to retrieve the palette pointer

        TPaletteAxis *palette = static_cast<TPaletteAxis*>(myHist->GetListOfFunctions()->FindObject("palette"));
        if (palette)
                palette->SetX1NDC(palette->GetX1NDC()+0.05); // shift x1 by 0.05 to the right
                palette->SetX2NDC(palette->GetX2NDC()+0.05); // shift x2 by 0.05 to the right

        gPad->Update(); gPad->Modified();