Draw TGraph from text file


I would like to draw a TGraph from a .txt file with 2 columns. What I already have is two arrays (let’s say x and y) in c++ code, but I’m stuck on plotting them. So I read the first column as one array and the second as the other, now I have to insert the right arrays instead of x and y, but I seem to miss something, because I always get an error. Any help on how to get the plot work would be appreciated :slight_smile:


	Int_t N = 100;
	string data_array[N][2];
	ifstream in_file("test.txt", ios::binary);
		cout << "File not opened..." << endl;

	for(int i=0; !in_file.eof(); i++){
		in_file >> data_array[i][0];
		in_file >> data_array[i][1];
	for(int i=0; i<N; i++){
		cout << data_array[i][0] << "  " << data_array[i][1] << '\n';

	//TGraph *plot = new TGraph(N,x,y);


1 Like

Try: TGraph *plot = new TGraph("test.txt"); plot->Draw("AL*");


That worked, thanks a lot! :smiley:

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