Home | News | Documentation | Download

Legend: fit curve entry and transparency

Dear all,
This is a legend question.

Q1: how to make the legend fill opaque, in a way the plot details (e.g., lines) on which it sits are not visible. I tried different colors, including white, but the legend fill is always transparent.

Q2: how to add a legend entry for the fit curve/line that is returned by FitFunction.cpp in the pseudo code below.

Main.cpp

   TH1* h1 = new TH1I("h1", "histo", 100, 0.0, 5.0);
   double xmin = 0.1;
   double xmax= 4.5;
   h1->Draw();
   PolFit(h1, polinom, xmin, xmax);
   TLegend *legend = new TLegend(0.1, 0.7, 0.5, 0.9);
   legend->SetFillColor(0);
   legend->AddEntry("h1", "hist", "l");
  // ideal: legend->AddEntry("fitted h1", "fit", "l")
   legend->Draw();
   

FunctionFit.cpp

double polinom(double *x, double *par) {
      return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
   }

  double PolFit(TH1* hist, double x_low, double x_up){
   TF1 *fitFunction = new TF1("fitFunction", polinom, x_low, x_up, 3);
   hist->Fit("fitFunction");
}

__
Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


A1: The legend should be “opaque” by default, i.e. unless you make it “hollow”, e.g. using somewhere something like “SetFillStyle(0)”.

A2: legend->AddEntry("fitFunction", "fitted h1", "l");

BTW. Before you try to “Fit” your histogram, you should set some “reasonable” initial values for all parameters of your function.

Thanks so much @Wile_E_Coyote.

For A1: I couldn’t find -explicitly- anything that SetFillStyle(0). Is there a way I can set the background to opaque?

It’s not about the “background”, it’s about the “legend”.

Make sure that the “legend” is drawn as the last one, i.e. that you do not have any histo->Draw("same") later.

Thank you very much! Drawing the legend last worked.

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