Plot legend for the fitted graph

Hi,
I am using polynomial fit for some data. I don’t want to plot the original data and only want to plot the fitted graph so I set gr2->SetLineWidth(0). But this creates a problem when I am trying to plot the legend for this graph, since it is not showing the dashed lines corresponding to the plot since for the original plot I have set width=0.

   gr2->SetLineWidth(0);
   gr2->SetLineStyle(9);
   gr2->Fit("pol8");
   gr2->GetFunction("pol8")->SetTitle(" ");
   gr2->GetFunction("pol8")->SetLineColor(kOrange+8);
   gr2->GetFunction("pol8")->SetLineWidth(3);
   gr2->GetFunction("pol8")->SetLineStyle(9);

In the legend, I am doing something like

  leg->AddEntry(gr2,"graph","l");

which writes the graph but without the dashed line associated with it.
Is there any way to hide the original plot but still use the legend for that?


ROOT Version: 6.08/06
Platform: Not Provided
Compiler: OSX


I don’t really understand your point: you want to hide a plot and nevertheless show it in the legend ? what’s the point of showing in a legend something which does not appear on the plot ?

Hi, I am hiding this:

   gr2->SetLineWidth(0);
   gr2->SetLineStyle(9);

but want to show this:

   gr2->Fit("pol8");
   gr2->GetFunction("pol8")->SetLineColor(kOrange+8);
   gr2->GetFunction("pol8")->SetLineWidth(3);
   gr2->GetFunction("pol8")->SetLineStyle(9);

therefore also want to show the legend for this.

Currently I am using a dummy TGraph to plot the legend for this.

void graphfit3(){
   Double_t x[] = {1., 2., 3.};
   Double_t y[] = {2., 3., 2.};
   auto g = new TGraph(3, x, y);

   g->Fit("gaus");
   TF1 *f = g->GetFunction("gaus");
   f->SetLineColor(kOrange+8);
   f->SetLineWidth(5);
   f->SetLineStyle(9);

   g->Draw("A*");

   auto l = new TLegend(0.65,0.7,0.89,0.89);
   l->AddEntry(f,"Fit result", "L");
   l->Draw();
}

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