# Is it possible to include fit parameters in a TLegend in a nice looking way?

**URL:** <https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100>\
**Category:** Newbie\
**Tags:** graphics\
**Created:** [April 26, 2020, 11:56am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100 "2020-04-26T11:56:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoschaHanel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@JoschaHanel](https://root-forum.cern.ch/u/JoschaHanel)\
**Post date:** [April 26, 2020, 11:56am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/1 "2020-04-26T11:56:43Z")

</div>

Hello,

I am wondering if it is possible to create one single legend for a graph with a fit function, where the legend contains the markers and the fit function line (with their respective descriptions) PLUS the fit parameters of the function.  
I’ve tried creating a TLegend for the graph and the fit function and also setting gStyle-\>SetOptFit(1111) separately, but getting the two boxes to align nicely is a lot of inconvenient fiddling around, especially if the fonts should be the same size, the alignment should be nice etc. So I am wondering, is there a (more or less) straightforward way of including the fit parameters in the TLegend (or an alternative to TLegend that can accomplish this)?

This is my code at the moment:

```auto
{
  TCanvas *myCanvas = new TCanvas("myCanvas","Window1", 1080,720);
  myCanvas -> SetGrid();

  TGraph *gr1 = new TGraph("data.txt");
  gr1 -> SetTitle("Autocorrelation fit function for 138#pm 3 kcts/s;#Delta t [ns];Counts");
  gr1 -> SetMarkerStyle(22);
  gr1 -> SetMarkerSize(1);
  gr1 -> SetMarkerColor(kBlue);
  gr1 -> GetXaxis() -> SetLimits(0,2400);

  TF1 *fitfunc = new TF1("fitfunc","[0]*(1-([1]+1)*exp(-[2]*abs(x-[3]))+[1]*exp(-[4]*abs(x-[3])))+[5]",0,2400);
  fitfunc -> SetParameters(738,0.49,0.079,2269,0.0086,100);
  fitfunc -> SetParNames("C_{1}","k","k_{1}","x_{0}","k_{2}","C_{0}");
  
  gr1 -> Fit(fitfunc,"R0");

  fitfunc -> SetLineColor(kBlue+2);
  fitfunc -> SetLineWidth(4);

  TLegend *leg = new TLegend(0.11,0.75,0.5,0.89);
  leg -> AddEntry(gr1, "Measured counts", "P");
  leg -> AddEntry(fitfunc, "Fit function", "L");
  leg -> SetBorderSize(1);

  
  // What follows is the option that I am currently using. It sort of gets everything to align on my screen, but seems rather inconvenient:
  gStyle -> SetOptFit(1111);
  gStyle -> SetStatBorderSize(1);
  gStyle -> SetStatX(0.5);
  gStyle -> SetStatY(0.75);
  gStyle -> SetStatW(0.2165);
  
  gr1 -> Draw("AP");
  fitfunc -> Draw("same");
  leg -> Draw("same");
}

```

My Idea was to include the fit parameters in the TLegend in some way like the following (replacing the entire gStyle section above):

```auto
for(int par=0;par<=5;par++){
     leg -> AddEntry(fitfunc->GetParName(par), Form("%g",fitfunc->GetParameter(par))," ");
  }

```

This displays the parameter values nicely; however it does NOT display the parameter names and I also don’t have any ideas on how I would add the errors.

Thanks in advance!  
Joscha

_ROOT Version:_ v6.20.04  
_Platform:_ Ubuntu 20.04  
_Compiler:_ Cling

* * *

---

<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:** [April 27, 2020, 5:23am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/2 "2020-04-27T05:23:32Z")

</div>

You can type the text you want to show in the legend: `Form("Value of par is = %g",fitfunc->GetParameter(par)...` and so on.  
To access fit results, read the [Fitting Histograms](https://root.cern.ch/root/htmldoc/guides/users-guide/FittingHistograms.html#result-of-the-fit) section of the User’s guide.

---

<div class="post-metadata">

**Author:** ![JoschaHanel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@JoschaHanel](https://root-forum.cern.ch/u/JoschaHanel)\
**Post date:** [April 29, 2020, 8:22am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/3 "2020-04-29T08:22:33Z")

</div>

Thanks for your reply!  
I have managed to access the parameters as well as their names; the problem I can’t find a solution for is how to add the parameter names to the “Object” column of the legend. Meaning the parameter Names should be in the same legend column as the representations of the markers from `gr1` and the function line from `testfunc`.  
Since `AddEntry` needs a pointer to some form of `TObject` as its first argument, I’ve tried

```auto
TObjString *par0Name = new TObjString("Value for Par0: "); 
leg -> AddEntry(par0Name, Form("%g",fitfunc->GetParameter(0))," ");

```

This runs without any errors or warnings and prints the VALUE of the parameter where I want it (though I still need to figure out how to add the errors). But the name of the Parameter doesn’t show up, no matter which options I use for `AddEntry`.  
Is it possible at all to add an object to the left column of the legend (in a visible way) if it’s not a `TAttLine`, `TAttMarker` or `TAttFill` Object?

---

<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:** [April 29, 2020, 8:26am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/4 "2020-04-29T08:26:11Z")

</div>

To add a entry in a Legend you need to have an object to be added.

---

<div class="post-metadata">

**Author:** ![JoschaHanel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@JoschaHanel](https://root-forum.cern.ch/u/JoschaHanel)\
**Post date:** [April 29, 2020, 8:31am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/5 "2020-04-29T08:31:19Z")

</div>

Does `par0Name` in my example above not count as an object? Or does the object _have_ to be of the `TAttLine`, `TAttMarker` or `TAttFill` kind?

---

<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:** [April 29, 2020, 8:34am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/6 "2020-04-29T08:34:41Z")

</div>

> [@JoschaHanel](#):
>
> object _have_ to be of the `TAttLine` , `TAttMarker` or `TAttFill` kind?

Of course the object must inherit from `TAttLine` , `TAttMarker` or `TAttFill`… otherwise how can you find the graphics attributes ?

---

<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:** [April 29, 2020, 8:38am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/7 "2020-04-29T08:38:02Z")

</div>

I am not sure using TLgend to draw fit parameters is a good idea anyway. Legend is very specific … and fit parameters do not have any graphical attributes. If you are not happy with the default display of fit parameters and cannot tune it the way you need, you will have to draw them yourself using TLatex for instance.

---

<div class="post-metadata">

**Author:** ![JoschaHanel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@JoschaHanel](https://root-forum.cern.ch/u/JoschaHanel)\
**Post date:** [April 29, 2020, 8:54am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/8 "2020-04-29T08:54:32Z")

</div>

> Of course the object must inherit from `TAttLine` , `TAttMarker` or `TAttFill` … otherwise how can you find the graphics attributes ?

That makes sense, thank you.  
There is one last idea that I just thought of: Is it possible to create a custom `TAttMarker` (that consists of the name of the respective parameter) and put that in the object column (with a reasonable amout of work)?  
Otherwise I am probably going to go with other options.

---

<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:** [April 29, 2020, 9:07am UTC](https://root-forum.cern.ch/t/is-it-possible-to-include-fit-parameters-in-a-tlegend-in-a-nice-looking-way/39100/9 "2020-04-29T09:07:11Z")

</div>

That sounds much more work than making a macro which align the legend and the fit box.
