Remove an Entry from a TLegend?

ROOT Version: 6.26/06
Platform: Scientific Linux 7.3 (Nitrogen)
Compiler: g++ (GCC) 12.1.0


Hello ROOTers!

I am looking for a way to remove an entry from a TLegend via a script. How can I accomplish this? I’ve tried RecursiveRemove, but that doesn’t seem to do it (see MWE below). Note that DeleteEntry does not work for me because it requires mouse interaction, and I need to do this in batch mode.

int testroot()
{
  TCanvas* can = new TCanvas("","",1000,1000);
  TLegend* leg = new TLegend(0.3,0.3,0.7,0.7);
  TH1F* hist = new TH1F("hist","",100,0,1);
  hist->SetMarkerStyle(20);
  hist->SetMarkerSize(2);
  hist->SetMarkerColor(kRed);
  leg->AddEntry(hist,"the histogram","p");
  hist->Draw("P HIST");
  leg->Draw();
  can->SaveAs("beforeRemove.pdf");
  leg->RecursiveRemove(hist);
  can->Clear();
  hist->Draw("P HIST");
  leg->Draw();
  can->SaveAs("afterRemove.pdf");
  return 0;
}

Many thanks for any help that can be provided!

Hi,

Thanks for reaching out!

I am trying to guess a bit the ultimate goal you are trying to accomplish, please bear with me if I write something in error.
Would deleting or letting go out of scope the histogram you do not need work for you?

int testroot()
{
  TCanvas* can = new TCanvas("","",1000,1000);
  TLegend* leg = new TLegend(0.3,0.3,0.7,0.7);
  TH1F* hist = new TH1F("hist","",100,0,1);
  hist->SetMarkerStyle(20);
  hist->SetMarkerSize(2);
  hist->SetMarkerColor(kRed);
  TH1F* hist2 = new TH1F("hist2","",100,0,1);
  hist2->SetMarkerStyle(30);
  hist2->SetMarkerSize(3);
  hist2->SetMarkerColor(kBlue);
  leg->AddEntry(hist,"the histogram","p");
  leg->AddEntry(hist2,"the histogram2","p");
  hist->Draw("P HIST");
  leg->Draw();
  can->SaveAs("beforeRemove.pdf");
  delete hist2;
  can->Clear();
  hist->Draw("P HIST");
  leg->Draw();
  can->SaveAs("afterRemove.pdf");
  return 0;
}

Cheers,
D

Thanks for the reply!

When I run your code as is, I still see both histograms on the legend after re-drawing…

You can try this:

{
    auto c = new TCanvas("c", "Canvas", 800, 600);

    auto h1 = new TH1F("h1", "Histogram 1", 10, 0, 10);
    auto h2 = new TH1F("h2", "Histogram 2", 10, 0, 10);
    h1->SetLineColor(kRed);
    h2->SetLineColor(kBlue);

    auto legend = new TLegend(0.1, 0.7, 0.3, 0.9);
    legend->AddEntry(h1, "Red Histogram", "l");
    legend->AddEntry(h2, "Blue Histogram", "l");

    h1->Draw();
    h2->Draw("SAME");
    legend->Draw();

    // Remove the  "Blue Histogram" from the legend
    auto entries = legend->GetListOfPrimitives();
    for (int i = 0; i < entries->GetSize(); ++i) {
        auto entry = dynamic_cast<TLegendEntry *>(entries->At(i));
        if (entry && strcmp(entry->GetLabel(), "Blue Histogram") == 0) {
            entries->Remove(entry);
            delete entry; 
            break;
        }
    }
}

Instead of the loop on all the legend entries, if you want to remove the last entry you can also use:

    auto entries = legend->GetListOfPrimitives();
    entries->RemoveLast();

Note also you can remove a legend entry interactively via the pop up menu you get by right clicking on the TLegend.

Odd. Yours is a rather old version, I was using 6.34.

Cheers,
D

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