TLegend and space use

Hello, I’m trying to optimise the use of space on a TCanvas and I’ve noticed that the text in a TLegend usually does not take up all the space available. Is there any way to find out how much space the markers and text in a TLegend take up in the x-direction, so I can put the remaining empty space to good use?

1 Like

do you have a small example showing what you mean ?

Hi, I’ve attached a macro that I hope explains what I want to do. First I create a TLegend, then it is obvious that I could have made a smaller TLegend. Then I changed the size of the TLegend by eye to get some of the space back. I want to be able to do this automatically, instead of having to do it by eye every time.


TLegend_example.C (2.78 KB)

I think you macro is not the right one. When I run it I get no legend.

Hmm, that’s odd. It works fine for me. In any case I’ve attached screenshots showing the two outputs- one bad that wastes space, and one good that uses space more effectively.

Thanks again


Ah ok … got it … you do not make pointers … there fore screen is not correct … but your print the pictures .

Ok the text size of each line is computed to fill the legend along y-axis.
the x size is free we do not adjust on it. If we may the size such the text will fill the box along X then the line will overlap…

Simply make your legend smaller along X

Hello again, that almost answers my question, but not quite… It’s easy to make the TLegend smaller on a single plot by eye, but what I want to be able to do is automate this resizing in a macro. That way I would not have to do this again and again for about 50 plots each time I write a paper, as I have to do at the moment. Am I correct in thinking that doing this in a macro is impossible with a TLegend?

To have more control you can fixe the text size yourself like in

   TH1F *h1 = new TH1F("h1","h1",100,-10,10);

   TLegend *leg = new TLegend(0.1,0.5,0.3,0.7);
   leg->AddEntry(h1,"A gaussian","LEP");

but if you have legends with 2 chars then legends with 15 chars and you want all this automatic you will have to do some logic according to the length of the text.

Hi, thanks again! I think that what you have suggested is probably as close we can get with the existing functionality of TLegend. I think I’ll request a method that returns the minimum horizontal size needed to display a TLegend so that in the future we can trivially get this kind of information and automatically resize the TLegend to save some space.



Hi, sorry to interrupt. I also get some problem about TLegend. It’s just the opposite of Aidan’s question. Sometimes, I need to make the legend more longer (as the different line-styles need a certain length to distinguish them). In that case, I need to take the whole TLegend range as long as possible.
But the canvas range is limit and there is still a lot of remaining space could not be used by the legend.
As shown in the plot, there is a huge white space in right range of the legend.

I’m not sure I understand what you mean, but if what you want is the symbols in the TLegend to use a bigger fraction of the horizontal space within the legend, use SetMargin:

 leg->SetMargin(0.4);   // try different fractions

Hi, thanks a lot. It’s very helpful for my question.