Multigraph Fit not showing in the Legend

Hi all,

I am trying to plot data from set of txt files and do a common fit using TMultiGraph Fit() The fit is valid and is also evident from the drawn fit function.

However, in the TLegend, box, the fit line does not appear. Can someone please help here

void do_fit() 
{


auto*gr1 = new TGraph("fwhm_sqr.txt", "%lg %lg");
gr1->SetMarkerStyle(8);
gr1->SetMarkerColor(kRed);

auto *gr2 = new TGraph("fwhm_sqr.txt", "%lg %*lg %lg");
gr2->SetMarkerStyle(8);
gr2->SetMarkerColor(kBlue);

auto g = new TMultiGraph();
g->Add(gr1);
g->Add(gr2);
g->Draw("ap");

g->Fit("pol1") ;

gPad->BuildLegend();

gStyle->SetOptFit(111);
}

Thanks a lot
.

Hello,

I am adding @couet in the loop.

Best,
Danilo

Hope it helps and always provide a minimal working script to help you out better!! Here you did not provide fwhm_sqr.txt file

void do_fit() 
{

	auto*gr1 = new TGraph("fwhm_sqr.txt", "%lg %lg");
	gr1->SetMarkerStyle(8);
	gr1->SetMarkerColor(kRed);

	auto *gr2 = new TGraph("fwhm_sqr.txt", "%lg %*lg %lg");
	gr2->SetMarkerStyle(8);
	gr2->SetMarkerColor(kBlue);

	auto g = new TMultiGraph();
	g->Add(gr1);
	g->Add(gr2);
	g->Draw("ap");

	TF1 *f1 = new TF1("f1", "pol1");
	f1->SetLineColor(kMagenta);

	g->Fit(f1);

	TLegend *leg = new TLegend(0.8,0.8, 0.9,0.9);
	leg->AddEntry(gr1, "graph 1", "p");
	leg->AddEntry(gr2, "graph 2", "p");
	leg->AddEntry(f1, "fit", "l");
	leg->Draw();

	gStyle->SetOptFit(111);

}
1 Like

Thanks a lot @Shiva_Prasad_Nayak for the solution. This add the first function entry to the legend,

Though not important, i am still a bit curious as to the issue with using BuildLegend() directly. Any cues on that will be highly appreciated.

Thank You

It works with BuildLegend too:

{
   auto c = new TCanvas();

   const Int_t size = 10;
   gStyle->SetOptFit(111);

   double x[size];
   double Y1[size];
   double Y2[size];

   for ( int i = 0; i <  size ; ++i ) {
      x[i] = i;
      Y1[i] = size - i;
      Y2[i] = size - 0.5 * i;
   }

   TGraph * gr1 = new TGraph( size, x, Y1 );
   gr1->SetName("gr1");
   gr1->SetTitle("graph 1");
   gr1->SetMarkerStyle(21);
   gr1->SetDrawOption("AP");
   gr1->SetLineColor(4);
   gr1->SetLineWidth(4);
   gr1->SetFillStyle(0);
   gr1->SetFillColor(4);

   TGraph * gr2 = new TGraph( size, x, Y2 );
   gr2->SetName("gr2");
   gr2->SetTitle("graph 2");
   gr2->SetMarkerStyle(22);
   gr2->SetMarkerColor(2);
   gr2->SetDrawOption("P");
   gr2->SetLineColor(3);
   gr2->SetLineWidth(4);
   gr2->SetFillStyle(0);

   auto g = new TMultiGraph();
   g->Add(gr1);
   g->Add(gr2);
   g->Fit("pol1");
   g->Draw("alp");

   c->BuildLegend();
}

Thanks a ton @couet, for the nice reproducer

However, I think in the plot above the fit function (pol1), denoted by the red line, is still not being added to the legend.

Ah yes, it is the fit. It looks like BuildLegend don’t see it. To be checked why.

Try with this

void do_fit() 
{

	auto*gr1 = new TGraph("fwhm_sqr.txt", "%lg %lg");
	gr1->SetMarkerStyle(8);
	gr1->SetMarkerColor(kRed);

	auto *gr2 = new TGraph("fwhm_sqr.txt", "%lg %*lg %lg");
	gr2->SetMarkerStyle(8);
	gr2->SetMarkerColor(kBlue);

	auto g = new TMultiGraph();
	g->Add(gr1);
	g->Add(gr2);
	g->Draw("ap");

	TF1 *f1 = new TF1("f1", "pol1");
	f1->SetLineColor(kMagenta);
	f1->SetTitle("whatever title you want to appear in legend");
    f1->Draw("same");
	g->Fit(f1);

	gPad->BuildLegend();

}

@Shiva_Prasad_Nayak, thanks a lot for the response. I’ll try getting back with the result of running the same by tomorrow.

I made a PR to fix this: Add the objects from the list of functions in the Legend. by couet · Pull Request #15179 · root-project/root · GitHub

1 Like

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