# Create legend for histogram created in for loop

**URL:** <https://root-forum.cern.ch/t/create-legend-for-histogram-created-in-for-loop/38429>\
**Category:** Newbie\
**Tags:** root\
**Created:** [March 16, 2020, 2:47pm UTC](https://root-forum.cern.ch/t/create-legend-for-histogram-created-in-for-loop/38429 "2020-03-16T14:47:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 16, 2020, 2:47pm UTC](https://root-forum.cern.ch/t/create-legend-for-histogram-created-in-for-loop/38429/1 "2020-03-16T14:47:51Z")

</div>

ROOT Version: 6.18/04  
Platform: Linux

I am trying to add a legend to a plot that consists of 3 histograms that were created in a for loop, they are called ratio[i], where i goes from 0 to 2, and refers to the elements in the following vector:

```auto
std::vector<std::string> Cut = {"L_J20", "L_J30", "L_J40"}

```

I have given each histogram a different colour by writing:

```auto
if (i == 0) ratio[i]->SetLineColor(kRed);
else if (i == 1) ratio[i]->SetLineColor(kBlue);
else ratio[i]->SetLineColor(kYellow);

```

This works, but I was wondering if there is a quicker way to do this (in the form of a loop perhaps). But then my main question is, how do I now create a legend for these histograms? I have tried:

```auto
auto legend = new TLegend (.1, .7, .3, .9);
legend->AddEntry(ratio[0], "J20")
legend->AddEntry(ratio[1], "J30")
legend->AddEntry(ratio[2], "J40")
legend->Draw("same")

```

This doesn’t work (it crashes, but I get no error). Anyone got any idea on how to solve this?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 16, 2020, 3:03pm UTC](https://root-forum.cern.ch/t/create-legend-for-histogram-created-in-for-loop/38429/2 "2020-03-16T15:03:36Z")

</div>

```auto
{
   TH1D *h[3];

   for (int i=0; i<3; i++) {
      h[i] = new TH1D(Form("h%d",i), Form("h%d",i), 100, -1, 1);
      h[i]->SetLineColor(i+3);
   }

   h[0]->Draw();
   h[1]->Draw("SAME");
   h[2]->Draw("SAME");

   auto legend = new TLegend(0.1,0.7,0.28,0.9);

   for (int i=0; i<3; i++) {
      legend->AddEntry(h[i], Form("J%d0",i+3), "l");
   }

   legend->Draw();
}

```

 ![Screenshot 2020-03-16 at 16.02.32](https://root-forum.cern.ch/uploads/default/original/3X/9/d/9dcc8a760ec132f7783e1f9d12fb51e2084b123f.png)

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 16, 2020, 3:14pm UTC](https://root-forum.cern.ch/t/create-legend-for-histogram-created-in-for-loop/38429/3 "2020-03-16T15:14:21Z")

</div>

That works! Thanks! What would I do, if I don’t want the J’s to be consecutive though? So, what would I do if I want to call my histograms J20, J40 and J75 for example?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 16, 2020, 3:33pm UTC](https://root-forum.cern.ch/t/create-legend-for-histogram-created-in-for-loop/38429/4 "2020-03-16T15:33:57Z")

</div>

If there is no logic in the legend’s comments you want tu use, it will not be possible to use the logic I showed you. You need to make an array of comments instead.
