# Plot with removed points, SetPoint and RemovePoint not working

I’m having a problem trying to plot a TGraphAsymmErrors plot in a loop, where sometimes points need to be removed if they are less than a certain value. The y, eyl and eyh (y, y error low, and y error high) are stored in 2D arrays.

``````float x[14] = {,,,}; // not including numbers for sake of clarity and ease
float exl[14] = {,,,};
float exh[14] = {,,,};
float y[14][46] = {,,,};
float eyl[14][46] = {,,,};
float eyh[14][46] = {,,,};

for(int j=0; j<46; j++)
{
TCanvas *c1 = new TCanvas("c1","Cross Section",10,10,900,500);
gr = new TGraphAsymmErrors(14);

for(int i=0;i<14;i++)
{
if(y[i][j] > 1e-7)
{
gr->SetPoint(i,x[i],y[i][j];
gr->SetPointError(i,exl[i],exh[i],eyl[i][j],eyh[i][j];
}
}

gr->Draw("AP");
}``````

This works for one loop, but as soon as I have points that don’t meet the IF statement, then points stick around from the previous loop. I fixed this by using

but then I get garbage in the absent points, rather than them simply being absent.

``````if(y[i][j] < 1e-7)
{
gr->RemovePoint(i);
}``````

after the SetPoint section, then I get duplicate points (a second y value for same x value), using the x value from the next lowest defined point but a different y value (from the previous loop). If I reset the plot (using the gr->Set(0) command), I get garbage data in those removed points.

If I use the IF statements for both SetPoint and RemovePoint, the same thing happens with the duplicate points (and I get garbage data with gr->Set(0) between main loop iterations).

Any strategies I can try to get around this? I want the points completely removed from the plot in a given iteration of the main loop, not just set to 0 or whatever’s in memory.

Seems to me RemovePoint() works fine:

``````void graph_removepoint() {
const Int_t n = 10;
Double_t x[n], y[n];

for (Int_t i=0;i<n;i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2)-5;
}

TGraph *gr = new TGraph(n,x,y);
printf("Number of points before RemovePoint: %d\n", gr->GetN());
gr->RemovePoint(5);
printf("Number of points after RemovePoint:  %d\n", gr->GetN());
gr->Draw("A*L");
}``````