KAHIBARO
Discord Login Register

8.7. Saving Plots

Table of Contents

PNG

Once you have a canvas with your plot, the most straightforward way to save it is to use the TCanvas::SaveAs method. In an interactive ROOT session, if your canvas is called c1, you can run:

cpp
c1->SaveAs("plot.png");

ROOT looks only at the file extension to decide the output format, so the .png suffix tells ROOT to produce a PNG image.

PNG is a raster format, so the image is stored as pixels. The apparent resolution of the output depends on the canvas size in pixels. You control this when you create the canvas:

cpp
TCanvas *c1 = new TCanvas("c1", "My canvas", 800, 600); // width, height in pixels

If you need higher resolution, create a larger canvas before drawing and saving. Enlarging the PNG later in an external tool will not add detail and usually makes the image blurry.

You can also call SaveAs from the graphical user interface. After drawing on a canvas window, choose File then Save As... then select plot.png as the filename. This performs exactly the same operation as the C++ call.

PNG is usually a good choice for quick checks, slides that accept raster graphics, or sharing screenshots of plots. For journal publications or detailed editing in vector graphics tools you will usually prefer PDF or SVG.

ROOT chooses the output format only from the filename extension in SaveAs. To get a PNG file, the name must end in .png.

PDF

To save a plot as a vector graphic, which can scale cleanly to any size, you can save the canvas as a PDF:

cpp
c1->SaveAs("plot.pdf");

This produces a vector file as long as the objects drawn on the canvas are vector primitives. Standard ROOT histograms, graphs, axes, and text are written as vector instructions. When you zoom in on the PDF in a viewer, lines and text remain sharp rather than turning into pixels.

PDF is the most common format for publication-quality plots. Most journals and conferences accept or require PDF figures. It is also convenient if your entire paper is written in LaTeX, because LaTeX handles PDF figures very well.

If you have multiple canvases, you save each one independently:

cpp
c1->SaveAs("hist.pdf");
c2->SaveAs("fit.pdf");

The file you pass to SaveAs is overwritten without warning if it already exists, so choose filenames carefully.

You can also save plots from the canvas GUI to PDF by selecting File then Save As... then a name with .pdf. For consistent appearance across multiple figures, configure your global styles and axis formatting before drawing and saving, so that every canvas uses the same fonts, sizes, and margins.

To get a vector output, always use a vector format such as .pdf. Do not rely on resizing a .png if you need high quality in print.

SVG

SVG is another vector format that ROOT can produce. It is especially useful if you want to edit a figure in tools that work with SVG, such as Inkscape or many web and graphics applications. To save a canvas as SVG:

cpp
c1->SaveAs("plot.svg");

Again, ROOT uses the .svg extension to choose the output format. The internal representation is vector based, similar in spirit to PDF, but stored as an XML text file.

One advantage of SVG is that it is easy to open the file in a vector editor and adjust individual elements. For example, you can tweak line thickness, change colors, or move labels manually while keeping the underlying geometry unchanged. This is useful when you want very fine control over the appearance of a plot before including it in a presentation, poster, or article.

In some cases, very complex canvases that contain many objects can produce large SVG files. This is normal, because each line segment and text object appears explicitly in the XML. For most typical physics plots, file sizes remain manageable.

As with other formats, you can use the canvas GUI: choose File then Save As... and specify an .svg filename.

If you plan to edit plots in a vector graphics editor, save as .svg or .pdf from ROOT. Raster formats like .png or .jpg do not preserve individual plot elements for editing.

ROOT format

Besides image and vector formats, you can save the canvas itself as a ROOT object in a .root file. This does not create an image. Instead, it stores the whole TCanvas and its contents so you can reopen it later in ROOT and still interact with it.

To save a canvas into a ROOT file:

cpp
TFile *f = new TFile("plots.root", "RECREATE");
c1->Write("c1");
f->Close();

Here plots.root contains a canvas named "c1". When you open that file in a later ROOT session you can retrieve the canvas:

cpp
TFile *f = TFile::Open("plots.root");
TCanvas *c1 = (TCanvas*)f->Get("c1");
c1->Draw();

You now have a live canvas again. You can modify it, change drawing options, style the contents differently, or save new images. This is particularly helpful if you want to preserve not only the visible result but also the exact ROOT objects used to create the figure.

There is also a shortcut from the canvas GUI. In the canvas window, choose File then Save As... then give a filename with .root. ROOT creates a file containing the canvas. Internally this is similar to writing the canvas with Write().

You can combine this with image saving. For example, you can both write the canvas to a ROOT file for future editing and also export a PDF for a paper:

cpp
TFile *f = new TFile("plots.root", "RECREATE");
c1->Write("myPlot");
f->Close();
c1->SaveAs("myPlot.pdf");

Use .root files when you want to reopen and continue working with a plot in ROOT. Use .png, .pdf, or .svg when you need a finished figure for documents, slides, or the web.

Views: 10

Comments

Please login to add a comment.

Don't have an account? Register now!