Automatic Coloring of a TMultiGraph and Fit Curves


ROOT Version: 6.20
Platform: MacOS
Compiler: Clang


Dear ROOT Developers,

I enjoy and actively employ the automatic coloring feature of a TMultiGraph according to a palette simply by drawing it with the options PMC, PML, etc… However, if the TGraph(Errors) passed to the TMultiGraph are fitted with some function, the fit line is drawn with the color manually selected in the program (e.g red by default).

Is it currently possible in ROOT to apply this automatic coloring feature to the fit lines too, that is having each TGraph and its fit function of the same color in a TMultiGraph?

Best regards,
Loris

Can you provide a small example macro showing what you mean ?

Yes indeed. Here is a short macro explaining what I mean. As it is right now, the markers are drawn according to the palette, whereas the fit lines are default red. Is there a way to have the latter drawn according to the palette?

example_root_automatic_colors.C (1.0 KB)

Ah yes, I see what you mean. The Fit is not the graph itself it is part of the list of functions in the graph but is not the graph. The PLC option will apply on the graph if you draw it with option “L” . The fit is an other “line” and you may want to see both; The graph line and the fit line. If we apply the PLC color on the the fit line then the graph line and the fit line will have the same color and it will be difficult to differentiate them…

But you can get what you want if:

just after:

    mg.Draw("AP PMC PLC PFC");

you do:

    gPad->Update();
    int c1 = g1.GetLineColor();
    int c2 = g2.GetLineColor();
    TF1 *f1 = (TF1*)g1.GetListOfFunctions()->FindObject("pol1");
    f1->SetLineColor(c1);
    TF1 *f2 = (TF1*)g2.GetListOfFunctions()->FindObject("pol1");
    f2->SetLineColor(c2);

Thank you very much for the solution! It works smoothly!

In my final application, the TGraphErrors are created and added to the TMultiGraph in a different scope, hence the snippet needs a tweak. I leave here the working macro:

    // --- SELECT THE PALETTE
    gStyle -> SetPalette (100);



    // --- PREPARE SOME DATA
    std::vector <double> x      = {0., 1., 2., 3., 4., 5.};
    std::vector <double> y1     = {0., 1., 2., 3., 4., 5.};
    std::vector <double> y2     = {0., 2., 4., 6., 8., 10.};
    std::vector <double> err_x  = {0., 0., 0., 0., 0., 0.};
    std::vector <double> err_y  = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1};



    // --- CREATE TCanvas AND TMultigraph
    TCanvas c ("c", "c", 1000, 800);
    TMultiGraph mg;



    // --- DIFFERENT SCOPE --- //
    {   
        // --- CREATE THE TGraphs
        TGraphErrors *g1 = new TGraphErrors (x.size(), &x[0], &y1[0], &err_x[0], &err_y[0]);
        TGraphErrors *g2 = new TGraphErrors (x.size(), &x[0], &y2[0], &err_x[0], &err_y[0]);

        g1->SetMarkerStyle(20);
        g2->SetMarkerStyle(20);

        g1->SetTitle("Graph #1");
        g2->SetTitle("Graph #2");
        g1->SetName("g_1");
        g2->SetName("g_2");

        // --- FIT
        g1->Fit("pol1");
        g2->Fit("pol1");

        // --- ADD
        mg.Add(g1);
        mg.Add(g2);
    }
    // --- --------------- --- //



    mg.Draw("AP PMC PLC PFC");



    // --- MODIFIED SOLUTION --- //
    gPad -> Update();
    auto g1 = (TGraphErrors*) mg.GetListOfGraphs()->FindObject("g_1");
    auto c1 = g1->GetLineColor();
    auto f1 = (TF1*) g1 -> GetListOfFunctions()->FindObject("pol1");
    f1 -> SetLineColor(c1);

    auto g2 = (TGraphErrors*) mg.GetListOfGraphs()->FindObject("g_2");
    auto c2 = g2->GetLineColor();
    auto f2 = (TF1*) g2 -> GetListOfFunctions()->FindObject("pol1");
    f2 -> SetLineColor(c2);



    // --- DRAW THE CANVAS ---
    c.SetGrid();
    c.BuildLegend(0.15, 0.6, 0.4, 0.8);
    c.DrawClone();

As a final note, I would suggest evaluating the possibility of implementing in a future ROOT release some option similar to PLC for the functions of a TGraphs. Personally, I never really use the L option when drawing TMultiGraphs, whereas I more often happen to plot TGraphs and fit functions of some quantity for different parameters (the line slope, in this example).

Thank you very much, once more!
Loris

1 Like

That may creates some extra complexity. Some people like the option L or C. It is nice to be able to have different colours for the TGraph and the fitted function. The piece of code I sent you can be made more general and reused elsewhere.

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