# Warning from TLegend and how to solve it

**URL:** <https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835>\
**Category:** ROOT\
**Tags:** graphics, root\
**Created:** [May 7, 2026, 10:24pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835 "2026-05-07T22:24:04Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [May 7, 2026, 10:24pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/1 "2026-05-07T22:24:05Z")

</div>

Hello everyone,

I am using ROOT’s graphics libraries for my C++ application. Now, to create a legend for the plot, I am using TLegend. It works fine and I have a nice legend on my plot but when I execute the program, I get the following warning:

```auto
Warning in <TLegend::Paint>: Legend too large to be automatically placed; a default position is used

```

Note that this warning is not thrown when I `make`. Now, I am okay with the warnings, but the problem is that TLegend is taking too long (~5 seconds) to decide where to put the TLegend box. Also, this complain from TLegend is fake as I only have 3 entries. Have a look at the plot below, there is nothing special

 ![Screenshot from 2026-05-07 19-19-03](https://root-forum.cern.ch/uploads/default/original/3X/f/8/f8a8bf4dacae304e0de4ee69e74ee5a93d4478e6.png)

Now, I am happy with the default position as long as it does not take long for TLegend to decide. Is there anyway to tell TLegend to take the default position from the beginning?

Best,  
Divyang.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [May 8, 2026, 4:32am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/2 "2026-05-08T04:32:52Z")

</div>

Can you provide the code you use to create this (at least the relevant parts), or a minimal running reproducer?  
Here is the relevant part of the `TLegend` source for the message you are seeing:

> **[ROOT: graf2d/graf/src/TLegend.cxx Source File](https://root.cern/doc/master/TLegend_8cxx_source.html#l00560)**

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 8, 2026, 6:51am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/3 "2026-05-08T06:51:12Z")

</div>

This message means that you are trying to let `TLegend` place itself automatically on the plot. The algorithm attempts to find some free space on the plot without hiding any graphics, and when it fails, it produces this message.

The solution is to place the legend yourself at an appropriate position instead of letting `TLegend` determine the position automatically. [See the `TLegend` documentation](https://root.cern/doc/master/classTLegend.html) for more details (search _“The legend can be placed automatically”_).

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [May 8, 2026, 10:30am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/4 "2026-05-08T10:30:19Z")

</div>

Here is the minimal version: [Test.zip](https://root-forum.cern.ch/uploads/short-url/4RpJR8lP6MDo97DPMtFdcZGHzaQ.zip) (3.8 KB)

Please go to `source/Profile_func.cc`, where I am using TLegend. Comment line number 130 (`leg->Draw("same");`) to remove the warning message.

OS: Ubuntu 20.04.6 LTS x86\_64  
ROOT: 6.24/08  
compiler: g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 8, 2026, 10:32am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/5 "2026-05-08T10:32:39Z")

</div>

Thanks. I am looking

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 8, 2026, 10:36am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/6 "2026-05-08T10:36:28Z")

</div>

I get errors:

```auto
root [0] 
Processing Profile_func.cc...
input_line_9:2:2: error: no matching constructor for initialization of 'Profile_func'
 Profile_func() /* .x tries to invoke function `Profile_func` */
 ^
/Users/couet/Downloads/Test/source/Profile_func.cc:15:15: note: candidate constructor not viable: requires single argument 'E', but no arguments were provided
Profile_func::Profile_func(double E)
              ^ ~~~~~~~~

```

What is the procedure to run your test ?

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [May 8, 2026, 10:42am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/7 "2026-05-08T10:42:00Z")

</div>

Sorry for not providing the steps to run.

1. Go to `Test` directory, where `makefile` and `main.cc` are located
2. run `make` to create executable named `test_4_leg`
3. `./test_4_leg` to run

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 8, 2026, 10:57am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/8 "2026-05-08T10:57:02Z")

</div>

do:

```auto
  {
    can1->cd(1);
    auto leg = new TLegend(0.1,0.7,0.48,0.9);
...
    leg->Draw();
  }

```

this code will disable the automatic placement and should remove the warning.  
note “same” is not a option for TLegend::Draw

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [May 8, 2026, 11:02am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/9 "2026-05-08T11:02:09Z")

</div>

If you don’t draw gr2 the warning (and the delay) goes too, so it looks like it is doing something that it shouldn’t. With a very simple graph there’s no problem:

```auto
 //...
    TGraph *gr2 = new TGraph();
/*
    for (size_t i=0; i<=Get_E().size(); i++)
    {
      double dmmy[1] = {Get_E()[i]};
      Add_sigm_pp(FitFunc4_sigm_pp(dmmy, fit_para) );
      gr2->AddPoint(Get_E()[i], Get_sigm_pp()[i]);
    }
*/
    gr2->AddPoint(200,2.3);
    gr2->SetMarkerStyle(47);
    gr2->SetMarkerSize(2);
    gr2->SetMarkerColor(kGreen);
    gr2->Draw("P");
    leg->AddEntry(gr2, "Prjctl enrgy");

    //! comment line below to remove the warning
    leg->Draw();
//...
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 8, 2026, 11:16am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/10 "2026-05-08T11:16:02Z")

</div>

With the following code, there is no delay and no message:

```auto
void Profile_func::Set_Vis_FitFuncs()
{
  //
  TCanvas *can1 = new TCanvas();
  can1->Divide(3,2);

  //-------------------------------------
  {
    can1->cd(1);
    auto leg = new TLegend(0.1,0.7,0.48,0.9);

    std::vector<double> x = Get_data_E();
    std::vector<double> y = Get_data_sigm_pp();

    TGraph *gr = new TGraph();
    for (std::size_t i=0; i<x.size(); i++) {gr->AddPoint(x[i], y[i]);}
    gr->SetMarkerStyle(21);
    gr->SetMarkerColor(kBlue);
    gr->SetMarkerSize(1.);
    gr->Draw("AP");
    leg->AddEntry(gr, "data");

    TF1 *fit = new TF1("fit", FitFunc4_sigm_pp, 35, 1005, 6);
    gr->Fit(fit, "QR+");
    leg->AddEntry(fit, "fit");

    double fit_para[6] = {fit->GetParameter(0), fit->GetParameter(1),
                          fit->GetParameter(2), fit->GetParameter(3),
                          fit->GetParameter(4), fit->GetParameter(5)};

    TGraph *gr2 = new TGraph();
    for (size_t i=0; i<=Get_E().size(); i++)
    {
      double dmmy[1] = {Get_E()[i]};
      Add_sigm_pp(FitFunc4_sigm_pp(dmmy, fit_para) );
      gr2->AddPoint(Get_E()[i], Get_sigm_pp()[i]);
    }
    gr2->SetMarkerStyle(47);
    gr2->SetMarkerSize(2);
    gr2->SetMarkerColor(kGreen);
    gr2->Draw("same P");
    leg->AddEntry(gr2, "Prjctl enrgy");

    leg->Draw();
    gPad->Update();
    gPad->Modified();
  }

}

```

adjust the legend position as you like …

 ![Screenshot 2026-05-08 at 13.29.03](https://root-forum.cern.ch/uploads/default/original/3X/9/1/91370d17d7cd3d0bac6aaec3b878ac044e3c2dcd.jpeg)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 8, 2026, 11:36am UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/11 "2026-05-08T11:36:37Z")

</div>

```cpp
    // ...
    for (size_t i = 0; i < Get_E().size(); i++)
    // ...
    gr2->Draw("P");
    // ...
    leg->Draw();
    // ...

```

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [May 8, 2026, 12:42pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/12 "2026-05-08T12:42:05Z")

</div>

Thank you @couet . Defining the position of TLegend box solved the issue, that is,

```auto
TLegend *leg = new TLegend(0.5,0.7, 0.9,0.9);

```

I noticed that in your code the following lines were added

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

```

Even if I don’t add these lines, things work just fine. So, for my knowledge, I want to know, is it a best practice to update the `gPad`?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 8, 2026, 1:07pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/13 "2026-05-08T13:07:07Z")

</div>

It was to force the graphics to display. On Mac (my machine) it is required to have thw window updated. Depending on which platform you are working it might not be necessary.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 8, 2026, 1:30pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/14 "2026-05-08T13:30:43Z")

</div>

> [@Divyang](#):
>
> Defining the position of TLegend box solved the issue

No, it just conceals [bugs in your code](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/11).

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [May 8, 2026, 2:58pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/15 "2026-05-08T14:58:10Z")

</div>

@Wile_E_Coyote I am sorry, I just recently started using ROOT libraries for C++ project. Therefore, I am not aware of the best practice.

I understand that solely relying on error and warning messages is not enough for a good code. If there is any issue with the code, please let me know. I would be happy to learn.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 8, 2026, 3:01pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/16 "2026-05-08T15:01:52Z")

</div>

I’ve already shown you [the three lines that need improvements](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/11).

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [May 8, 2026, 3:20pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/17 "2026-05-08T15:20:56Z")

</div>

oh! I see it now. The for loop should be

```auto
for (std::size_t i=0; i<Get_E().size(); i++)

```

not `i<=Get_E().size()`. Sorry for the silly mistake. Now `TLegend` can find the best position without delay or warnings. Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 22, 2026, 3:21pm UTC](https://root-forum.cern.ch/t/warning-from-tlegend-and-how-to-solve-it/64835/18 "2026-05-22T15:21:18Z")

</div>

Closed on May 22, 2026, 3:21pm UTC

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