Different Line Color for different Plots

Good day!
I have a lot of data files and I want to plot it on the same canvas. This function work pretty well, but unfortunately it can’t change the graph Line Color. So I have all graphs with the same color.
How I can fix this problem?

`void Plot_Gr()
{
    char fileStr[20];
    TCanvas *c1 = new TCanvas();
    TGraph *gr = new TGraph();
    for (int fileName = 0; fileName < 20; fileName++)
    {
        gr->SetMarkerStyle(fileName);
        gr->SetLineColor(fileName);
        fstream file;
        sprintf(fileStr, "%d.txt", fileName);
        file.open(fileStr, ios::in);
        while (1)
        {
            double x, y;
            file >> x >> y;
            gr->SetPoint(gr->GetN(), x, y);
            if(file.eof())
            break;
        }
        file.close();
        gr->Draw("APL");
    }

}`

ROOT Version: 6.18/04
Platform: ubuntu_21.04
Compiler: gcc


It works for me:

{
   auto c = new TCanvas("c","A Simple Graph without axis",700,500);
   const Int_t n = 10;
   auto gr = new TGraph();
   gr->SetTitle("A Simple Graph Example");
   gr->GetXaxis()->SetTitle("X axis");
   gr->SetEditable(kFALSE);
   gr->SetMarkerColor(3);
   gr->SetLineColor(2);
   Double_t x, y;
   for (Int_t i=0;i<n;i++) {
      x = i*0.123;
      y = 10*sin(x+0.2);
      gr->AddPoint(x,y);
   }
   gr->Draw("A*L");
}

But your macro does not plot all the graphs on the same plot. Only the last one will remain (option “A”). You may want to do something like:

void Plot_Gr() {
    char fileStr[20];
    auto mg = new TMultiGraph();
    TGraph *gr;
    for (int fileName = 0; fileName < 20; fileName++) {
        gr = new TGraph();
        gr->SetMarkerStyle(fileName+1);
        gr->SetLineColor(fileName+1);
        fstream file;
        sprintf(fileStr, "%d.txt", fileName);
        file.open(fileStr, ios::in);
        while (1) {
            double x, y;
            file >> x >> y;
            gr->SetPoint(gr->GetN(), x, y);
            if(file.eof())
            break;
        }
        file.close();
        mg->Add(gr);
    }
    mg->Draw("APL");
}
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.