# TLegend make a box on a TF1

**URL:** <https://root-forum.cern.ch/t/tlegend-make-a-box-on-a-tf1/60223>\
**Category:** Newbie\
**Created:** [July 16, 2024, 2:18pm UTC](https://root-forum.cern.ch/t/tlegend-make-a-box-on-a-tf1/60223 "2024-07-16T14:18:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bc.pereira](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bc.pereira/32/37108_2.png) [@bc.pereira](https://root-forum.cern.ch/u/bc.pereira)\
**Post date:** [July 16, 2024, 2:18pm UTC](https://root-forum.cern.ch/t/tlegend-make-a-box-on-a-tf1/60223/1 "2024-07-16T14:18:25Z")

</div>

Hello,

I am trying to make a box over a TF1 in the Legend.

I have something like this:

where fstat is a TF1

legendcell = new TLegend(0.67,0.53,0.98,0.7+0.015\*(years.size()));  
fstat-\>SetLineColor(429);  
fstat-\>SetFillColor(5);  
fstat-\>SetFillStyle(1001);

legendcell-\>AddEntry(fstat, “#bf{” + legend + “}” ,“fl”);

legendcell-\>Draw();

The problem is that this also creates a filling in the TGraph.  
Is there any way to create a box on the TLegend without changing the drawing on the canvas?

PS: I also tried to do fstat-\>SetFillStyle(0); after the drawing of the legend but this also removes the box from the TLegend

My root version is 6.28/00

I really appreciate any help you can provide.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [July 16, 2024, 2:33pm UTC](https://root-forum.cern.ch/t/tlegend-make-a-box-on-a-tf1/60223/2 "2024-07-16T14:33:03Z")

</div>

Hi Beatriz,

Could you provide a minimal reproducer for this plot for us to be able to help?

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![bc.pereira](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bc.pereira/32/37108_2.png) [@bc.pereira](https://root-forum.cern.ch/u/bc.pereira)\
**Post date:** [July 16, 2024, 2:53pm UTC](https://root-forum.cern.ch/t/tlegend-make-a-box-on-a-tf1/60223/3 "2024-07-16T14:53:00Z")

</div>

I can try:

graph = (TGraphErrors\*) file-\>Get(graphname);

TMultiGraph \*statgraph = new TMultiGraph();

statgraph = Add(graph)

TF1 \*fstat = new TF1(“fit”,fitfunc\_form.c\_str());

fstat = (TF1\*) graph-\>GetFunction(“fit”);

TLegend _legendcell;  
legendcell = new TLegend(0.67,0.53,0.98,0.7+0.015_(years.size()));

fstat-\>SetLineColor(429);  
fstat-\>SetFillColor(5);  
fstat-\>SetFillStyle(1001);

legendcell-\>AddEntry(fstat, “#bf{” + legend + “}” ,“fl”);

legendcell-\>Draw();

statgraph-\>Draw(“ap”);

Something like this helps?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [July 16, 2024, 3:29pm UTC](https://root-forum.cern.ch/t/tlegend-make-a-box-on-a-tf1/60223/4 "2024-07-16T15:29:54Z")

</div>

Hi,

Thanks a lot! However, this is still extracted from a context where variables are defined… Can you make it a macro we can execute perhaps?

Cheers,  
D

---

<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:** [July 17, 2024, 5:34am UTC](https://root-forum.cern.ch/t/tlegend-make-a-box-on-a-tf1/60223/5 "2024-07-17T05:34:17Z")

</div>

You could add an empty entry and then manually draw a [TBox](https://root.cern/doc/master/classTBox.html) in the position you want (and you’ll have to find out the coordinates), see as a starting guide the second example in the [TLegend](https://root.cern/doc/master/classTLegend.html) documentation.  
Another option, maybe simpler, is to make a fake function (or histogram) with the line and fill styles as the one you want, but at a position outside the limits of the frame you are plotting, and add this function, _instead of the real one_, to the legend (you have to `Draw` it, otherwise the fill doesn’t show on the legend; drawing it outside the frame means it is not visible, but the entry is correctly added to the legend); e.g.

```auto
{
   auto c1 = new TCanvas("c1","c1",600,500);
   gStyle->SetOptStat(0);
 
   auto h1 = new TH1F("h1","TLegend Example",200,-10,10);
   h1->FillRandom("gaus",30000);
   h1->SetFillColor(kGreen);
   h1->SetFillStyle(3003);
   h1->Draw();
 
   auto f1=new TF1("f1","x",-20,-20); // outside the range of the histogram shown (-10 to 10)
   f1->SetLineColor(kBlue);
   f1->SetLineWidth(2);
   f1->SetFillColor(5);
   f1->SetFillStyle(1001);
   f1->Draw("same");
 
   auto legend = new TLegend(0.1,0.7,0.48,0.9);
   legend->AddEntry(h1,"Histogram filled with random numbers","f");
   legend->AddEntry("f1","Dummy function","fl");
   legend->Draw();
}

```

![c1](https://root-forum.cern.ch/uploads/default/original/3X/c/a/ca50c302bd94ca95c6fa3aab79b6cfff1177b0ed.png)

---

<div class="post-metadata">

**Author:** ![bc.pereira](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bc.pereira/32/37108_2.png) [@bc.pereira](https://root-forum.cern.ch/u/bc.pereira)\
**Post date:** [July 21, 2024, 10:55am UTC](https://root-forum.cern.ch/t/tlegend-make-a-box-on-a-tf1/60223/6 "2024-07-21T10:55:08Z")

</div>

Hello,

Yes, this works, thank you very much. I thought there was another way to do but maybe not.

Kind regards,
