Question

I am trying to create a graph and save it as an image. I am required to use ROOT. I created the graph with

TGraph graph = TGraph(xvect, yvect);

but now I'm stuck on how to get that saved as a png (or other image format). I'm using a linux machine if that makes a difference. Also, if anyone knows link to the documentation that describes the method for writing the graph to an image file, I could figure it out myself from there, but I have been unsuccessful in finding that in the documentation thus far.

Was it helpful?

Solution

TCanvas*c1 = new TCanvas();
graph->Draw();
c1->Print("name.png");

Will certainly work in the cint shell. It may need some fine-tuning to work in compiled code.

You'll find all this basic stuff exhaustively covered in the on-line tutorials and HowTos. Also see the documentation in general.

OTHER TIPS

TCanvas*cvs = new TCanvas();
graph->Draw();
cvs->SaveAs("name.png");

SaveAs has been my go to function for saving graphs in root. As a side note the online documentation is very useful as dmckee said. class list

The complete macro will be:

TCanvas *c1 = new TCanvas();
const Int_t n = 10;
Double_t xvect[n];
Double_t yvect[n];
.... initialize xvect and y vect
TGraph graph = TGraph(n, xvect, yvect);
graph->Draw("al"); // draw the graph as a line (see the ROOT wen site for more option)
c1->SaveAs("c1.png"); // many other formats are available (PS, PDF, JPEG etc...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top