# TLegend not taking TF1 line color

**URL:** <https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563>\
**Category:** Newbie\
**Tags:** python\
**Created:** [February 10, 2023, 3:34am UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563 "2023-02-10T03:34:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![allanfgodoi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/allanfgodoi/32/32419_2.png) [@allanfgodoi](https://root-forum.cern.ch/u/allanfgodoi)\
**Post date:** [February 10, 2023, 3:34am UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563/1 "2023-02-10T03:34:23Z")

</div>

Greetings.

I have a multigraph with 4 graphs with a fit with a different color within. Every fit is set like the model:

```auto
fit1 = TF1("fit1", "pol1")
fit1.SetLineColor(kRed)

```

But when I call the legend with

```auto
legend = TLegend(0.1,0.67,0.3,0.9)
legend.SetHeader("Legend", "C")
legend.AddEntry("fit1", "First node", "l")
legend.AddEntry("fit2", "Second node", "l")
legend.AddEntry("fit3", "Third node", "l")
legend.AddEntry("fit4", "Fourth node", "l")
legend.AddEntry("fit5", "Fifth node", "l")
legend.Draw()

```

All of their lines are in black.

![image](https://root-forum.cern.ch/uploads/default/original/3X/8/8/8853a7827e672b71c89a967ab850a939faba25e1.png)

What can I do to fix it?

* * *

_ROOT Version:_ 6.26/10  
_Platform:_ Ubuntu 22.04

* * *

---

<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:** [February 10, 2023, 7:50am UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563/2 "2023-02-10T07:50:43Z")

</div>

Welcome to the ROOT forum.

I will look.

---

<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:** [February 10, 2023, 7:57am UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563/3 "2023-02-10T07:57:31Z")

</div>

```auto
void legtf1()
{
   auto f = new TF1("f","x*x",-2,2);
   f->SetLineColor(kBlue);
   f->SetLineWidth(4);
   f->Draw();

   auto legend = new TLegend(0.1,0.67,0.3,0.9);
   legend->SetHeader("Legend", "C");
   legend->AddEntry(f, "First node", "l");

   legend->Draw();
}

```

 ![Screenshot 2023-02-10 at 08.56.53](https://root-forum.cern.ch/uploads/default/original/3X/9/9/991523feeaf1da0603863fe1409a3434a60f334f.jpeg)

---

<div class="post-metadata">

**Author:** ![allanfgodoi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/allanfgodoi/32/32419_2.png) [@allanfgodoi](https://root-forum.cern.ch/u/allanfgodoi)\
**Post date:** [February 10, 2023, 1:29pm UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563/4 "2023-02-10T13:29:17Z")

</div>

Hi, couet.

Thanks for your answer, I fixed it without using multigraph, but drawing five different graphs with the “same” parameter and it worked.

Have a nice day!

---

<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:** [February 10, 2023, 1:47pm UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563/5 "2023-02-10T13:47:08Z")

</div>

It should work with TMultiGraph. Can you provide your script ?  
Drawing graphs without option A (SAME ins not a graph option) is not convinient because the axis scales are not automatically computed.

---

<div class="post-metadata">

**Author:** ![allanfgodoi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/allanfgodoi/32/32419_2.png) [@allanfgodoi](https://root-forum.cern.ch/u/allanfgodoi)\
**Post date:** [February 10, 2023, 1:54pm UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563/6 "2023-02-10T13:54:35Z")

</div>

Of, course. That’s how I was doing using TMultiGraph

```auto
from ROOT import *
from array import array

title = "" # CANVAS TITLE
x_title = "" # X AXIS CANVAS TITLE
y_title = "" # Y AXIS CANVAS TITLE
filename = "multigraph.pdf" # FILE NAME AND FORMAT FOR THE CANVAS PRINT

c1 = TCanvas()
c1.SetGrid()

mg = TMultiGraph()
mg.SetTitle("Frequency x 1/Wavelength in Different Nodes")

list_pos1 = [9, 6.5, 4.5, 4]
list_pos2 = [30, 24, 19, 16]
list_pos3 = [52, 41, 34, 28]
list_pos4 = [73.5, 59.5, 49, 41]
list_pos5 = [95.5, 75.5, 63.5, 53]
list_f = [800, 1000, 1200, 1400]

n = 4

f, pos1, pos2, pos3, pos4, pos5 = array("d"), array("d"), array("d"), array("d"), array("d"), array("d")

for i in list_pos1:
    pos1.append(100/(4*i))
for i in list_pos2:
    pos2.append(100/(2*i))
for i in list_pos3:
    pos3.append(100/(2*i))
for i in list_pos4:
    pos4.append(100/(2*i))
for i in list_pos5:
    pos5.append(100/(2*i))
for i in list_f:
    f.append(i)

fit1 = TF1("fit1", "pol1")
fit1.SetLineColor(kRed)
fit2 = TF1("fit2", "pol1")
fit2.SetLineColor(kYellow)
fit3 = TF1("fit3", "pol1")
fit3.SetLineColor(kBlue)
fit4 = TF1("fit4", "pol1")
fit4.SetLineColor(kViolet)
fit5 = TF1("fit5", "pol1")
fit5.SetLineColor(kGreen)

gr1 = TGraph(n, f, pos1)
gr1.SetMarkerStyle(22)
gr1.SetMarkerColor(kBlack)
gr1.SetMarkerSize(0.8)
gr1.Fit("fit1")

gr2 = TGraph(n, f, pos2)
gr2.SetMarkerStyle(22)
gr2.SetMarkerColor(kBlack)
gr2.SetMarkerSize(0.8)
gr2.Fit("fit2")

gr3 = TGraph(n, f, pos3)
gr3.SetMarkerStyle(22)
gr3.SetMarkerColor(kBlack)
gr3.SetMarkerSize(0.8)
gr3.Fit("fit3")

gr4 = TGraph(n, f, pos4)
gr4.SetMarkerStyle(22)
gr4.SetMarkerColor(kBlack)
gr4.SetMarkerSize(0.8)
gr4.Fit("fit4")

gr5 = TGraph(n, f, pos5)
gr5.SetMarkerStyle(22)
gr5.SetMarkerColor(kBlack)
gr5.SetMarkerSize(0.8)
gr5.Fit("fit5")

mg.Add(gr1)
mg.Add(gr2)
mg.Add(gr3)
mg.Add(gr4)
mg.Add(gr5)
mg.Draw("AP")
mg.GetXaxis().SetTitle("Frequency (Hz)")
mg.GetYaxis().SetTitle("1/Wavelength (m^{-1})")

legend = TLegend(0.1,0.67,0.3,0.9)
legend.SetHeader("Legend", "C")
legend.AddEntry("fit1", "First node", "l")
legend.AddEntry("fit2", "Second node", "l")
legend.AddEntry("fit3", "Third node", "l")
legend.AddEntry("fit4", "Fourth node", "l")
legend.AddEntry("fit5", "Fifth node", "l")
legend.Draw()

c1.Print(filename)

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 10, 2023, 2:39pm UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563/8 "2023-02-10T14:39:36Z")

</div>

Try:

```python
legend.AddEntry(fit1, "First node", "l")
legend.AddEntry(fit2, "Second node", "l")
legend.AddEntry(fit3, "Third node", "l")
legend.AddEntry(fit4, "Fourth node", "l")
legend.AddEntry(fit5, "Fifth node", "l")

```

---

<div class="post-metadata">

**Author:** ![allanfgodoi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/allanfgodoi/32/32419_2.png) [@allanfgodoi](https://root-forum.cern.ch/u/allanfgodoi)\
**Post date:** [February 11, 2023, 12:17am UTC](https://root-forum.cern.ch/t/tlegend-not-taking-tf1-line-color/53563/9 "2023-02-11T00:17:06Z")

</div>

This way it works!

Thanks, Wile.
