Plotting data from a text file onto a TGraph

Hi, I am trying to read in a text file that has two columns and plot it on a TGraph. I wrote out some code that casts it into a vector and then plots it however I am getting a blank document. Here is my code:

void plotNewIso(){
  Double_t x1[16], y1[16];
  std::ifstream in("New-Isotopes_C.txt");
  std::vector<std::pair<int, int>> vec;
  int P, N;
  while(in >> P && in >> N){
    vec.push_back((std::make_pair(P, N)));
  }

  int n = vec.size();
  for(int i = 0; i<n; i++){
    x1[i] = vec[i].first;
    y1[i] = vec[i].second;
  }
  TGraph* gr = new TGraph(n,x1,y1);
  TCanvas *c4 = new TCanvas("c4", "New Carbon",0,8,0,8);
  gr->Draw("AC");

  c4->Print("plots/New_Isotopes.pdf[");
  c4->Print("plots/New_Isotopes.pdf]");
}

The “graphing” part looks correct. Are you sure n, x1 and y1 are correct ?
We cannot run your macro because you did not post the .txt file.

Here I will share the text file sorry about that.

New-Isotopes_C.txt (64 Bytes)

We can make it much simpler:

void plotNewIso(){
  TGraph* gr = new TGraph("New-Isotopes_C.txt");
  gr->Draw("AC");
}

I am still getting a totally blank page. Does it have to do with my TCanvas? I am using Cygwin so I need to make it into a pdf like I did in order to see it. Is there an import I am missing for that?

If you execute exactly these two lines you get a blank page ?

note your TCanvas is wrong … well … at least very small and not very useful :

  TCanvas *c4 = new TCanvas("c4", "New Carbon",0,8,0,8);

Only 8x8 pixels…

void plotNewIso(){
  auto c = new TCanvas();
  TGraph* gr = new TGraph("New-Isotopes_C.txt");
  gr->Draw("AL*");
  c->Print("plotNewIso.pdf");
}

produces :
plotNewIso.pdf (13.9 KB)

Oh ok thank you that was a misunderstanding on my part it works now

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