Weird black Background On TLegend

I am making a multigraph from ascii files.
I use TLegend to recognise each plot but the output contains some weird black background on each entry.

You can see those weird black backgrounds…
My code is

#include "Riostream.h"
#include <cstdio>
#include <iostream>
#include <fstream>

# include "TROOT.h"
# include "TGraphErrors.h"
# include "TStyle.h"
# include "TMultiGraph.h"
# include "TF1.h"
# include "TLegend.h"
# include "TPaveStats.h"
# include "TArrow.h"
# include "TLatex.h"
# include "TPaveText.h"
# include "TText.h"
# include "TPavesText.h"

#include "TString.h"
#include "TSystem.h"
#include "TInterpreter.h"
#include "TFile.h"
#include "TH1.h"
#include "TNtuple.h"
#include "TCanvas.h"
void basic() {

   gROOT->SetStyle("Plain");
   gStyle->SetOptStat(1111);
   gStyle->SetOptFit(1111);
   
   TCanvas *c = new TCanvas("c", "c");
   c->SetFillColor(5);
   c->SetFrameFillColor(10);
   
   TMultiGraph * mg = new TMultiGraph("mg","mg");
   
   TGraph *g1=new TGraph("Fpp0pa0", "%lg %lg"); 
   g1->SetLineColor(2);
   
   TGraph *g2=new TGraph("Fpp0pa0pa1", "%lg %lg"); 
   g2->SetLineColor(3);
         
   TGraph *g3=new TGraph("Fpp0pa0pa1pa2", "%lg %lg"); 
   g3->SetLineColor(4);
   
   TGraph *g4=new TGraph("Fpp0pa0pa1pa2pa3", "%lg %lg"); 
   g4->SetLineColor(7);
   
   TGraph *g5=new TGraph("Fpp0pa0pa1pa2pa3pa4", "%lg %lg"); 
   g5->SetLineColor(6);
   
   mg->Add(g1);
   mg->Add(g2);
   mg->Add(g3);
   mg->Add(g4);
   
   //mg->Add(g5);
   
   g5->Draw();
   mg->Draw();
   c->BuildLegend();

   //return c3;
   
   //g1->Draw();
   //h1->Fit("gaus","W",NULL,350,800);
   //h1->GetFunction("gaus")->SetLineColor(kRed);
   
  //TPaveStats *st = ((TPaveStats*)(h1->GetListOfFunctions()->FindObject("stats")));
   
   c->SetLogy();
   c->SaveAs("19F.pdf");
}

Something also weird is that this code is working perfectly on windows, while on ubuntu, just a yellow canvas is printed…

TLegend *leg = c->BuildLegend();
leg->SetFillStyle(0);

Thank you very much for your reply.
The thing is that your code doesn’t fix this issue.

I also tried to change styles, but it doesn’t seem to change at all.
What’s going on??? :blush:

Ah yes sorry, you mean the black background on individual legend…

here root.cern.ch/root/html534/TPad.h … uildLegend you see that all the attributes are taken into account by this method. 2 solutions in your case:

  1. specify the fill style for each graph (now filled and black).
  2. do not use buildlegend and make the legend yourself.

I used

TLegend *legend1 = new TLegend(0.7,0.8,0.9,0.9);
   legend1->AddEntry(g1,"Fpp0pa0","g1");
   legend1->AddEntry(g2,"Fpp0pa0pa1","g2");
   legend1->Draw();

but the output is again-in a different way-weird

void basic() {

   TCanvas *c = new TCanvas("c", "c");

   TMultiGraph * mg = new TMultiGraph("mg","mg");

   TGraph *g1=new TGraph(2);
   g1->SetLineColor(2);
   g1->SetFillColor(0);

   TGraph *g2=new TGraph(2);
   g2->SetLineColor(3);
   g2->SetFillColor(0);

   TGraph *g3=new TGraph(2);
   g3->SetLineColor(4);
   g3->SetFillColor(0);

   TGraph *g4=new TGraph(2);
   g4->SetLineColor(7);
   g4->SetFillColor(0);

   TGraph *g5=new TGraph(2);
   g5->SetLineColor(6);
   g5->SetFillColor(0);

   mg->Add(g1);
   mg->Add(g2);
   mg->Add(g3);
   mg->Add(g4);
   mg->Add(g5);

   mg->Draw();
   TLegend *leg = c->BuildLegend();
   leg->SetFillStyle(0);

   c->SetLogy();
}

I used your previous code, but there are still black backgrounds on each entry.
I also used

TLegend *legend1 = new TLegend(0.7,0.8,0.9,0.9);
   legend1->AddEntry(g1,"Fpp0pa0");
   legend1->AddEntry(g2,"Fpp0pa0pa1");
   legend1->SetFillStyle(0);
   legend1->Draw();

but the problem remains… Could that be a bug?

If I execute the macro I sent you I get the following picture.
Can you execute the exact macro I sent you and show me what you get ?
On which platform are you running ? with which root version ?


If I use your code, I get exactly what you posted before.
However, when I load the data from the ascii files the black backgrounds appear.
I am ruuning root 5.34 on windows vista.

If I use your code, I get exactly what you posted before.
However, when I load the data from the ascii files the black backgrounds appear.

Loading the data from a file does not affect the graphics look.
You must have something else in your macro.

My macro is the following

#include "Riostream.h"
#include <cstdio>
#include <iostream>
#include <fstream>

# include "TROOT.h"
# include "TGraphErrors.h"
# include "TStyle.h"
# include "TMultiGraph.h"
# include "TF1.h"
# include "TLegend.h"
# include "TPaveStats.h"
# include "TArrow.h"
# include "TLatex.h"
# include "TPaveText.h"
# include "TText.h"
# include "TPavesText.h"

#include "TString.h"
#include "TSystem.h"
#include "TInterpreter.h"
#include "TFile.h"
#include "TH1.h"
#include "TNtuple.h"
#include "TCanvas.h"
void basic() {
  
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("basic.C","");
   dir.ReplaceAll("/./","/");
   
   gROOT->SetStyle("Plain");
   gStyle->SetOptStat(1111);
   gStyle->SetOptFit(1111);
   
   TCanvas *c = new TCanvas("c", "c");
   c->SetFillColor(5);
   c->SetFrameFillColor(10);
   
   TMultiGraph * mg = new TMultiGraph("mg","mg");
   
   TGraph *g1=new TGraph("p_in_Si.rng", "%lg %lg"); 
   g1->SetLineColor(2);
   
   TGraph *g2=new TGraph("alpha_in_Si.rng", "%lg %lg"); 
   g2->SetLineColor(3);
   
    mg->Add(g1);
    mg->Add(g2);
   mg->Draw("ALP");

   TLegend *leg = c->BuildLegend();
   leg->SetFillStyle(0);
}

BuildLegend a is a quick way to make the legend and all the attribute are taken into accound:
root.cern.ch/root/html534/TPad.h … uildLegend

so you should specify them all:

Ie:

   TGraph *g1=new TGraph("p_in_Si.rng", "%lg %lg");
   g1->SetLineColor(2);
   g1->SetFillStyle(0);
   g1->SetFillColor(0);

   TGraph *g2=new TGraph("alpha_in_Si.rng", "%lg %lg");
   g2->SetLineColor(3);
   g2->SetFillStyle(0);
   g2->SetFillColor(0);

For a more accurate way you should build the legend yourself with TLegend.

Note you had already the answer to your question in the first example I sent you.

Thank you very much!!!
It really works!!!

Just for reference, better solution is to use the third parameter of TLegend::AddEntry() (read more at root.cern.ch/doc/master/classTL … a7b8df2d24 )
The default value is “lpf”, which draws the point marker, line and fill (here seen as background). If you use only “p”, just the point marker is drawn; “lp” would draw both the marker and the line connecting them.
So use

TLegend leg(0.88, 0.70, 0.98, 0.80);
leg.AddEntry(myTGraph, "Mylabel", "pl");
leg.AddEntry(myTGraph2, "Mylabel2", "pl");
...

(or only “l”) for each TGraph instead of ->BuildLegend();