Can't draw 2 functions TF1 in the same canvas

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:

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");

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

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");
}

Thanks a lot. It works fine.

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

f1->GetXaxis()->SetTitle("Z");
f1->GetYaxis()->SetTitle("B(Z)");
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");
}
1 Like

Thank you so much.