TGraph, point in the origin

Dear all,

When I’m plotting an empty graph, I get a strange point in the origin plotted. How can I get rid of it? I’m using 5.20 on Linux, CINT/ROOT C/C++ Interpreter version 5.16.29.

A sample code is below:

// ROOT files
#include "TROOT.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "TAxis.h"

int main()
{
  const Int_t npts=10;

  TCanvas *c_graph = new TCanvas("c_graph","");
  TGraph  *g_graph = new TGraph(npts);

  g_graph->SetMarkerStyle(22);
  g_graph->SetMarkerSize(1.6);
  g_graph->SetMarkerColor(kRed);

  g_graph->GetXaxis()->SetLimits(-1.0,1.0);

  c_graph->cd(); g_graph->Draw("AP");

  c_graph->Print("graph_test.png");
}

The output is attached.
Thank you,
Slava


When doing: TGraph *g_graph = new TGraph(npts); npts points are allocated and the x[i],y[i] set to 0, hence your result.
I do not understand what you try to achieve in drawing a graph without defining the points. If you simply want to set the pad scale you should call pad.DrawFrame(xmin,ymin,xmax,ymax).

Rene

[quote=“brun”]When doing: TGraph *g_graph = new TGraph(npts); npts points are allocated and the x[i],y[i] set to 0, hence your result.
I do not understand what you try to achieve in drawing a graph without defining the points. If you simply want to set the pad scale you should call pad.DrawFrame(xmin,ymin,xmax,ymax).

Rene[/quote]

This is a simplification of a real life problem when there are situations when some/all point of a graph are not filled in a loop.

So, are you saying that whenever I don’t set some of the points from the number given in the constructor they will be defaulted to 0, consequently drawn at (0,0) and nothing can be done about that?

Slava

Yes, of course, the points must be defined before drawing the graph.
Why dont’ you define your graph with only the points that you can set.
You can always add new points to a TGraph.

Rene

1 Like

[quote=“brun”]Yes, of course, the points must be defined before drawing the graph.
Why dont’ you define your graph with only the points that you can set.
You can always add new points to a TGraph.

Rene[/quote]

Yes, I could use the default constructor and add only necessary points. Just I was not aware I could add them. After your reply I looked at TGraph in the reference guide again, but the only thing referring to adding points I could find was InsertPoint: Insert a new point at the mouse position This would not help me in the batch mode.

Slava

root.cern.ch/root/html/TGraph.ht … h:SetPoint
root.cern.ch/root/html/src/TGrap … tml#WjpSHE
root.cern.ch/root/html/TGraph.ht … emovePoint

etc