# Legend with Fit

**URL:** <https://root-forum.cern.ch/t/legend-with-fit/48497>\
**Category:** Newbie\
**Created:** [January 21, 2022, 8:10pm UTC](https://root-forum.cern.ch/t/legend-with-fit/48497 "2022-01-21T20:10:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MicheleScattola](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michelescattola/32/27810_2.png) [@MicheleScattola](https://root-forum.cern.ch/u/MicheleScattola)\
**Post date:** [January 21, 2022, 8:10pm UTC](https://root-forum.cern.ch/t/legend-with-fit/48497/1 "2022-01-21T20:10:34Z")

</div>

Hi, I am currently working on a plot. I have 1 canvas diveded in 2 plots. For both of the plots I am making a Fit with a function and I would like to add a legend to the canvas adding the 2 graphs and the Fit line as well. For some reason there is no legend on the canvas.  
My code looks something like this:

```auto
TCanvas * can = new TCanvas(...);
TGraph * f = new TGraph();
TGraph * g = new TGraph();

can->Divide(2,1);
can->cd(1);

... plotting of data ...

TF1 *Fit = new TF1("Fit","[0]/sqrt(x)",0,100000);
f->Fit(Fit);

f->Draw("ALP")

can->cd(2);

... plotting of data ...

g->Fit(Fit);

TLegend *leg = new TLegend(.....);
leg->AddEntry(Fit,"Fit","l");
leg->AddEntry(f,"Error f","lp");
leg->AddEntry(g,"Error g","lp");
leg->Draw();

g->Draw("ALP")

_ROOT Version:_ 6.24/06
_Platform:_ Ubuntu 21.04
_Compiler:_ g++
___
```

---

<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:** [January 21, 2022, 8:15pm UTC](https://root-forum.cern.ch/t/legend-with-fit/48497/2 "2022-01-21T20:15:38Z")

</div>

`g->Draw("ALP")` first clears the pad (destroying its previous contents), then draws the `g`

---

<div class="post-metadata">

**Author:** ![MicheleScattola](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michelescattola/32/27810_2.png) [@MicheleScattola](https://root-forum.cern.ch/u/MicheleScattola)\
**Post date:** [January 21, 2022, 10:38pm UTC](https://root-forum.cern.ch/t/legend-with-fit/48497/4 "2022-01-21T22:38:31Z")

</div>

Ok perfect so I will first draw f and g and then draw the legend and fit. Thank you very much, you have been saving me a whole afternoon of struggles 😜
