# Can't draw 2 functions TF1 in the same canvas

**URL:** <https://root-forum.cern.ch/t/cant-draw-2-functions-tf1-in-the-same-canvas/42002>\
**Category:** Newbie\
**Tags:** graphics\
**Created:** [October 26, 2020, 8:25am UTC](https://root-forum.cern.ch/t/cant-draw-2-functions-tf1-in-the-same-canvas/42002 "2020-10-26T08:25:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [October 26, 2020, 8:25am UTC](https://root-forum.cern.ch/t/cant-draw-2-functions-tf1-in-the-same-canvas/42002/1 "2020-10-26T08:25:48Z")

</div>

I want to draw 2 functions in the same canvas. I used option “same”, but it didn’t work properly. Could you help me, please? Here is my code:

```auto
void B_Z()
{
    
    TCanvas *c = new TCanvas("B(Z)", "B(Z) linear", 800, 600);
    c->SetGrid();
    c->SetTicks();

    TF1 *f1 = new TF1("f1", "0.088807", 6, 21);
    f1->SetLineColor(kRed-4);
    f1->Draw();

    TF1 *f2 = new TF1("f2", "0.08356", 21, 26);
    f2->SetLineColor(kAzure);
    f2->Draw("same");

```

 ![Screenshot_1](https://root-forum.cern.ch/uploads/default/original/3X/c/1/c1e73c8717185d36a2d06a09af8d9d4a65869c13.png)

---

<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:** [October 26, 2020, 8:50am UTC](https://root-forum.cern.ch/t/cant-draw-2-functions-tf1-in-the-same-canvas/42002/2 "2020-10-26T08:50:07Z")

</div>

The range on X is imposed by the first function and the second fonction is outside that range.  
A possible solution is:

```auto
void B_Z()
{
   TCanvas *c = new TCanvas("B(Z)", "B(Z) linear", 800, 600);
   c->SetGrid();
   c->SetTicks();

   c->DrawFrame(6,0,26,0.2);
   TF1 *f1 = new TF1("f1", "0.088807", 6, 21);
   f1->SetLineColor(kRed-4);
   f1->Draw("same");

   TF1 *f2 = new TF1("f2", "0.08356", 21, 26);
   f2->SetLineColor(kAzure);
   f2->Draw("same");
}

```

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [October 26, 2020, 9:47am UTC](https://root-forum.cern.ch/t/cant-draw-2-functions-tf1-in-the-same-canvas/42002/3 "2020-10-26T09:47:52Z")

</div>

Thanks a lot. It works fine.

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [October 26, 2020, 9:56am UTC](https://root-forum.cern.ch/t/cant-draw-2-functions-tf1-in-the-same-canvas/42002/4 "2020-10-26T09:56:18Z")

</div>

I have problem with titles of X, Y axis. I can not set titles for X and Y axis. I used the command.

```auto
f1->GetXaxis()->SetTitle("Z");
f1->GetYaxis()->SetTitle("B(Z)");

```

---

<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:** [October 26, 2020, 10:01am UTC](https://root-forum.cern.ch/t/cant-draw-2-functions-tf1-in-the-same-canvas/42002/5 "2020-10-26T10:01:21Z")

</div>

```auto
void B_Z()
{
   TCanvas *c = new TCanvas("B(Z)", "B(Z) linear", 800, 600);
   c->SetGrid();
   c->SetTicks();

   TH1F *f = c->DrawFrame(6,0,26,0.2);
   f->GetXaxis()->SetTitle("Z");
   f->GetYaxis()->SetTitle("B(Z)");

   TF1 *f1 = new TF1("f1", "0.088807", 6, 21);
   f1->SetLineColor(kRed-4);
   f1->Draw("same");

   TF1 *f2 = new TF1("f2", "0.08356", 21, 26);
   f2->SetLineColor(kAzure);
   f2->Draw("same");
}

```

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [October 27, 2020, 1:29am UTC](https://root-forum.cern.ch/t/cant-draw-2-functions-tf1-in-the-same-canvas/42002/6 "2020-10-27T01:29:54Z")

</div>

Thank you so much.
