Grid overlapping the plot's border

Hello! I am having a bit of trouble with painting grid on my plot. Here’s the example that reproduces what I am trying to show:

void draw_line_graphs() {

    TCanvas* c = new TCanvas("c", "Line Graphs Canvas", 800, 600);
    
    c->SetGrid();
    gStyle->SetGridColor(kGray);
    
    TMultiGraph* mg = new TMultiGraph();
    
    const int n_points = 100;
    double x1[n_points], y1[n_points];
    
    TRandom rand;
    for (int i = 0; i < n_points; ++i) {
        x1[i] = i;
        y1[i] = rand.Gaus(5, 2);
    }
    
    TGraph* graph1 = new TGraph(n_points, x1, y1);
    graph1->SetTitle("Graph 1");
    graph1->SetLineColor(kRed);
    graph1->SetLineWidth(2);
    
    double x2[n_points], y2[n_points];
    for (int i = 0; i < n_points; ++i) {
        x2[i] = i;
        y2[i] = rand.Gaus(10, 3);
    }

    TGraph* graph2 = new TGraph(n_points, x2, y2);
    graph2->SetTitle("Graph 2");
    graph2->SetLineColor(kBlue);
    graph2->SetLineWidth(2);
    
    mg->Add(graph1, "L"); 
    mg->Add(graph2, "L");
    mg->SetTitle("MultiGraph with Line Graphs;X axis;Y axis");
    

    mg->Draw("A"); 
    mg->SetMaximum(16);
    mg->GetXaxis()->SetLimits(0, 100);

    c->Update();

    c->SaveAs("line_graphs.pdf");
}

My goal here is to set plot’s limits so that they are not managed automatically, but rather take predetermined value (be it from any func, or I just set it myself). And I also need grey grid, because it’s fancier and it’s exactly what I need. However, when I set limits for x and y myself, the top and right border of my plot get overlapped by grey grid:

I haven’t found any methods or functionality that dictates the order of drawing for grids and borders. I see this as either changing the drawing order, so that grid is somehow “under” the border, or manually changing color of exactly the top and the most right grid dot row.

I would appreciate any help that would solve this issue!

Best regars,
Ruslan

I 'll check

1 Like
...
    mg->GetXaxis()->SetLimits(0., 100.);
    c->Modified();
    c->Update();
    gPad->RedrawAxis("F");
...
1 Like

Oh, I forgot that Update() method is used in my actual code. But gPad->RedrawAxis(“F”); did the trick, thank you!

1 Like

A little bit of follow-up, I’ve just noticed, that redrawing the axis also draws on top of existing TGraphs, which is a bit of an issue, since my lowest values often go to zero, and it is also where the lower limit of Y axis is located (intentionally). I am fine with data being on top of axis, but redrawing axis puts data behind them. Perhaps there’s a way to invoke the redrawing of TGraphs?

Can you provide an example reproducing what you are describing and show me what you do not like there ?

Sure! It goes kinda like this:

void draw_line_graphs() {

    TCanvas* c = new TCanvas("c", "Line Graphs Canvas", 800, 600);
    c->SetGrid();
    gStyle->SetGridColor(kGray);

    TMultiGraph* mg = new TMultiGraph();

    const int n_points = 100;
    double x1[n_points], y1[n_points];

    TRandom rand;
    for (int i = 0; i < n_points; ++i) {
        x1[i] = i;
        if (i >= n_points / 3 && i <= 2 * n_points / 3) {
            y1[i] = 0;
        } else {
            y1[i] = rand.Gaus(5, 2);
        }
    }

    TGraph* graph1 = new TGraph(n_points, x1, y1);
    graph1->SetTitle("Graph 1");
    graph1->SetLineColor(kRed);
    graph1->SetLineWidth(2);

    mg->Add(graph1, "L");
    mg->SetTitle("MultiGraph with Line Graphs;X axis;Y axis");

    mg->Draw("A");
    mg->GetXaxis()->SetLimits(0, n_points);
    mg->SetMaximum(16);
    mg->SetMinimum(0); 
    mg->GetYaxis()->SetLimits(0, 16); 

    c->Modified();
    c->Update();
    gPad->RedrawAxis("F");
    c->SaveAs("line_graphs.pdf");
}

Basically the same, I just added code for adding artificial zeros to data. Now, regarding the looks, as you can see, the grid is on top of the data.


Please, ignore the fact that data line is currently wider, my goal is to have red line on top of the black axis AND have proper display of grey grid (under the black axis)

so you want to see the red line overlapping the X axis ?

true, that is my goal

void draw_line_graphs() {
    TCanvas* c = new TCanvas("c", "Line Graphs Canvas", 800, 600);
    c->SetGrid();
    gStyle->SetGridColor(kGray);

    TMultiGraph* mg = new TMultiGraph();

    const int n_points = 100;
    double x1[n_points], y1[n_points];

    TRandom rand;
    for (int i = 0; i < n_points; ++i) {
        x1[i] = i;
        if (i >= n_points / 3 && i <= 2 * n_points / 3) {
            y1[i] = 0;
        } else {
            y1[i] = rand.Gaus(5, 2);
        }
    }

    TGraph* graph1 = new TGraph(n_points, x1, y1);
    graph1->SetTitle("Graph 1");
    graph1->SetLineColor(kRed);
    graph1->SetLineWidth(2);

    mg->Add(graph1, "L");
    mg->SetTitle("MultiGraph with Line Graphs;X axis;Y axis");

    mg->Draw("A");
    mg->GetXaxis()->SetLimits(0, n_points);
    mg->SetMaximum(16);
    mg->SetMinimum(0);
    mg->GetYaxis()->SetLimits(0, 16);

    c->Modified();
    c->Update();
    auto l1 = new TLine(gPad->GetUxmin(),gPad->GetUymax(),gPad->GetUxmax(),gPad->GetUymax()); l1->Draw();
    auto l2 = new TLine(gPad->GetUxmax(),gPad->GetUymin(),gPad->GetUxmax(),gPad->GetUymax()); l2->Draw();
}

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