TLegend font size alignment problems in TCanvas.Divide with TMultigraphs

So I tried to reduce the code as much as possible and problem is still there. Note that I used same parameters for the legend:

import ROOT


def test():

    c1 = ROOT.TCanvas()
    c1.Divide(4, 1, 0, 0)

    graphs = {}
    frames = {}

    for z in range(4):

        c1.cd(z+1)
        
        frames[z] = ROOT.gPad.DrawFrame(0., 0., 1., 1)
        
        graphs[z] = ROOT.TGraph()
        graphs[z].AddPoint(1,1)
        graphs[z].Draw("AP")

        c1.Update()

    c1.cd(1)
    legend = ROOT.TLegend(0.45,0.6,
                          0.88,0.8)
    legend.SetTextSize(0.06)
 
    legend.AddEntry(graphs[0], "legend")
    legend.AddEntry(graphs[1], "legend")
    legend.AddEntry(graphs[2], "legend")
        
    legend.Draw()
    c1.Update()
    c1.Print("aa.pdf")


test()

It seems like the vertical centering is not working as expected when there is a large empty space between entries. You can try using a larger font with the same legend box size, or a smaller box with the same font size, so that the text fills more of the vertical space; in both cases it looks better centered.

import ROOT

def test():
    c1 = ROOT.TCanvas()
    c1.Divide(4, 1, 0, 0)
    graphs = {}
    frames = {}
    for z in range(4):
        c1.cd(z+1)
        frames[z] = ROOT.gPad.DrawFrame(0., 0., 1., 1)
        graphs[z] = ROOT.TGraph()
        graphs[z].AddPoint(1,1)
        graphs[z].Draw("AP")
        c1.Update()
    c1.cd(1)
    legend1 = ROOT.TLegend(0.45,0.6,
                          0.88,0.8)
    legend1.SetTextSize(0.085)
    legend1.AddEntry(graphs[0], "legend")
    legend1.AddEntry(graphs[1], "legend")
    legend1.AddEntry(graphs[2], "legend")        
    legend1.Draw()

    c1.cd(2)
    legend2 = ROOT.TLegend(0.45,0.7,
                          0.88,0.8)
    legend2.SetTextSize(0.06)
    legend2.AddEntry(graphs[0], "legend")
    legend2.AddEntry(graphs[1], "legend")
    legend2.AddEntry(graphs[2], "legend")        
    legend2.Draw()

    c1.Update()
    c1.Print("ab.png")

test()

Now it has been solved, thanks to all, especially @dastudillo .

For the real project, I ended using

    legend = ROOT.TLegend(0.45,y0,0.88,0.8)
    legend.SetTextSize(0.06)

where y0=0.62 for three entries and y0=0.6 for four entries. Probably there is still some misalignment, but too small to be testing values (you need to focus to notice).

Should this behaviour be reported as a bug?

I do not think so because it really depends on the legend geometry. When the text size is not automatic it may occur that a correct alignment is not possible depending on the box size.

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