Keeping legend entry size constant

The legend entry size varies depending on the number of entries - compare 1 vs 2 entries:

When legends vary from a few entries to many the effect is not appealing.

I can control the text size by
legend -> SetTextSize(0.0045)

What remains now is to control the box. Is there some convenient way to control the legend entry size?

Here is the working script:

TLegend * legend = new TLegend(0.6, 0.6, 0.9, 0.9);
  TH1 * h1 = new TH1F("h1", "h1", 5, 0.0, 3.0);
  h1 -> SetFillColor(kGreen);
  TH1 * h2 = new TH1F("h2", "h2", 5, 0.0, 3.0);
  h2 -> SetFillColor(kBlue);
  TCanvas * c = new TCanvas();
  h1 -> Draw();
  h2 -> Draw("SAME");
  legend -> AddEntry(h1, h1 -> GetTitle(), "lpf");
  legend -> AddEntry(h2, h2 -> GetTitle(), "lpf");
  legend -> SetTextSize(0.045);                                                                                                             
  legend -> Draw("SAME");

So what is your question ? you are able to define a fixe legend entry size it seems.

The question is how to control the size of the green box in the third figure from above

It is automatic. It is equal to the height of the legend box divided by the number of rows.

I understand from your answer there is no simple way to control it.

As I said it is automatic. There is no setting for that. You can reduce the height of the legend box.

You can change the width of the green boxes with legend->SetMargin(x) where x is some float between 0 and 1.
As for the height, one workaround could be to add to the legend one or more histograms without name and symbol; for example:
legend -> AddEntry(h2," “,”")
(notice the space in the name’s quotation marks, otherwise “h2” is shown in the legend)
and then worry about the vertical placement of your entry within the legend box. This of course doesn’t provide lots of control and ultimately also depends on the automatic height calculation from the legend box size, but it’s something :grinning:

1 Like

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