How to add TLegend multiple markers per entry?

Hi,

I have a TCanvas w/ Ten curves. Two curves represent DATA while the other Eight corresponds to MC Models (Four for 13 TeV and Four for 7 TeV Energy). For 13 TeV data I use solid markers while for 7 TeV I use the same shape and color marker but leave the marker as open. I’d like to have only Five entries in the TLegend but somehow alert the observer of the plot the difference in the open vs solid markers like:

image

How to do like this shown in the picture above?

Thanks
Sheheryar

There is not direct way with TLegend but I can try to produce some example

1 Like

Oh great. Thanks for your help.
I just thought may be if I specify them separately and then change their location so that they becomes align in single lines…is it possible?

Let me try to produce an example… it will be easier

1 Like

Ok. That will help alot.

File multiplemarkerslegend.C

TLegend *legend1, *legend2;

void doublelegendinit(double x1, double y1, double x2, double y2) {
   double dx = 0.02;
   legend1 = new TLegend(x1, y1, x2, y2);
   legend1->SetLineWidth(0);
   legend1->SetFillStyle(0);
   legend1->Draw();
   legend2 = new TLegend(x1-dx, y1, x2-dx, y2);;
   legend2->SetLineWidth(0);
   legend2->SetFillStyle(0);
   legend2->Draw();
}

void doublelegendaddentry(TGraph *g1, TGraph *g2, TString l) {
   legend1->AddEntry(g1,l.Data(),"P");
   legend2->AddEntry(g2,"","P");
}


void multiplemarkerslegend() {

   auto g1 = new TGraph();
   g1->AddPoint(0.1, 10.0);
   g1->AddPoint(0.2,  7.0);
   g1->AddPoint(0.5,  4.0);
   g1->AddPoint(1.4,  0.5);

   g1->SetMarkerStyle(20);
   g1->Draw("ALP");

   auto g2 = new TGraph();
   g2->AddPoint(0.2, 10.3);
   g2->AddPoint(0.3,  7.3);
   g2->AddPoint(0.6,  4.3);
   g2->AddPoint(1.5,  0.8);

   g2->SetMarkerStyle(24);
   g2->Draw("LP");

   doublelegendinit(0.5,0.7,0.7,0.85);
   doublelegendaddentry(g1,g2,"#pi^{#pm}");
}
root[0] .x multiplemarkerslegend.C

gives:

1 Like

And if I include the same code in the example you deleted I get:

TLegend *legend1, *legend2;

void doublelegendinit(double x1, double y1, double x2, double y2) {
   double dx = 0.02;
   legend1 = new TLegend(x1, y1, x2, y2);
   legend1->SetLineWidth(0);
   legend1->SetFillStyle(0);
   legend1->Draw();
   legend2 = new TLegend(x1-dx, y1, x2-dx, y2);;
   legend2->SetLineWidth(0);
   legend2->SetFillStyle(0);
   legend2->Draw();
}

void doublelegendaddentry(TGraphErrors *g1, TGraphErrors *g2, TString l) {
   legend1->AddEntry(g1,l.Data(),"P");
   g2->SetTitle("");
   legend2->AddEntry(g2,"","P");
}

void Figure() {
   auto c1 = new TCanvas("c1", "Fig-1", 800, 900);
   c1->SetLeftMargin(0.2);
   c1->SetLogx();
   c1->SetLogy();

   // draw a frame to define the range for the upper pad
   TH1F *frame = c1->DrawFrame(0.08, 0.0000000101, 21., 400);
   frame->SetYTitle("1/N_{INEL} #times d^{2}N/(2#pip_{T}dydp_{T}) [(GeV/c)^{-1}])");
   frame->GetYaxis()->SetTitleOffset(1.75); // Adjust the offset to center the title
   frame->GetYaxis()->CenterTitle(true);

   //13 TeV------------------------------------------------------------------------------------------------------------------------------
   // EPOS-LHC
   const int n1_points = 4;
   double x1_vals[n1_points] = {1,5,10,15};
   double y1_vals[n1_points] = {0.123457, 0.097534, 0.135487, 0.22235};
   double x1_errs[n1_points] = {0,0,0,0};
   double y1_errs[n1_points] = {0,0,0,0};

   TGraphErrors* graph1 = new TGraphErrors(n1_points, x1_vals, y1_vals, x1_errs, y1_errs);
   graph1->SetMarkerColor(kBlue+1);
   graph1->SetMarkerStyle(20);
   graph1->SetMarkerSize(1.4);
   graph1->Draw("p");

   //7 TeV------------------------------------------------------------------------------------------------------------------------------
   // EPOS-LHC
   const int n2_points = 4;
   double x2_vals[n2_points] = {1,5,10,15};
   double y2_vals[n2_points] = {0.0123457, 0.0097534, 0.0135487, 0.022235};
   double x2_errs[n2_points] = {0,0,0,0};
   double y2_errs[n2_points] = {0,0,0,0};

   TGraphErrors* graph2 = new TGraphErrors(n2_points, x2_vals, y2_vals, x2_errs, y2_errs);
   graph2->SetMarkerColor(kBlue+1);
   graph2->SetMarkerStyle(24);
   graph2->SetMarkerSize(1.4);
   graph2->Draw("p");

   doublelegendinit(0.5,0.3,0.7,0.45);
   doublelegendaddentry(graph1,graph2,"#pi^{#pm}");
}

1 Like

That Worked. Thanks for your time.

1 Like

Hi,
there is a simpler way to do it,
using TLegend::SetNColumns(2).
See the attached file.
multiMarkerLegend.C (586 Bytes)

1 Like

I want to label right margin of y-axis and also want to show division bars. How to do this?

Yes this is an other possibility

Not sure I understand what you want exactly… can you show a picture ?

Your code with the two columns solution:

void Figure() {
   auto c1 = new TCanvas("c1", "Fig-1", 800, 900);
   c1->SetLeftMargin(0.2);
   c1->SetLogx();
   c1->SetLogy();

   // draw a frame to define the range for the upper pad
   TH1F *frame = c1->DrawFrame(0.08, 0.0000000101, 21., 400);
   frame->SetYTitle("1/N_{INEL} #times d^{2}N/(2#pip_{T}dydp_{T}) [(GeV/c)^{-1}])");
   frame->GetYaxis()->SetTitleOffset(1.75); // Adjust the offset to center the title
   frame->GetYaxis()->CenterTitle(true);

   //13 TeV------------------------------------------------------------------------------------------------------------------------------
   // EPOS-LHC
   const int n1_points = 4;
   double x1_vals[n1_points] = {1,5,10,15};
   double y1_vals[n1_points] = {0.123457, 0.097534, 0.135487, 0.22235};
   double x1_errs[n1_points] = {0,0,0,0};
   double y1_errs[n1_points] = {0,0,0,0};

   TGraphErrors* graph1 = new TGraphErrors(n1_points, x1_vals, y1_vals, x1_errs, y1_errs);
   graph1->SetMarkerColor(kBlue+1);
   graph1->SetMarkerStyle(20);
   graph1->SetMarkerSize(1.4);
   graph1->Draw("p");

   //7 TeV------------------------------------------------------------------------------------------------------------------------------
   // EPOS-LHC
   const int n2_points = 4;
   double x2_vals[n2_points] = {1,5,10,15};
   double y2_vals[n2_points] = {0.0123457, 0.0097534, 0.0135487, 0.022235};
   double x2_errs[n2_points] = {0,0,0,0};
   double y2_errs[n2_points] = {0,0,0,0};

   TGraphErrors* graph2 = new TGraphErrors(n2_points, x2_vals, y2_vals, x2_errs, y2_errs);
   graph2->SetMarkerColor(kBlue+1);
   graph2->SetMarkerStyle(24);
   graph2->SetMarkerSize(1.4);
   graph2->Draw("p");

   graph1->SetTitle("");
   TLegend *leg = new TLegend(0.5,0.7,0.7,0.85);
   leg->SetNColumns(2);
   leg->SetLineWidth(0);
   leg->AddEntry(graph1,"","P");
   leg->AddEntry(graph2,"#pi^{#pm}","P");
   leg->Draw();
}

1 Like

image

Like this.

One question more:
How can we control the errors. In my data the error touches the x-axis. I want to control it. Is there any solution for this?

image

See in this picture. The error bars are touching the x-axis.

Also how these error bars can be converted to shaded lines like: in the following picture

image

Change the minimum of the Y axis.

That’s errors drawn as boxes ? You can change the fill color to a lighter color