ROOT使用TCanvas类来绘制画布,在平常的使用中,如果在绘制TGraph,TFunction,THistogram是不调用该类,则ROOT自动初始化一个默认的TCanvas。但是如果想要将多张图绘制到一张画布上,就需要自己初始化画布。本篇介绍TCanvas的基本用法。
一、继承关系-Inheritance
如图所示。TCanvas继承自TPad。
二、构造函数-Constructor
第一类构造函数,常用。
TCanvas::TCanvas ( const char * name,
const char * title,
Int_t ww,
Int_t wh
)
第二类构造函数,常用。
TCanvas::TCanvas ( const char * name,
const char * title,
Int_t wtopx,
Int_t wtopy,
Int_t ww,
Int_t wh
)
以上两类构造函数的参数列表如下:
name | canvas name |
title | canvas title |
wtopx,wtopy | are the pixel coordinates of the top left corner of the canvas (if wtopx < 0) the menubar is not shown) |
ww | is the canvas size in pixels along X |
wh | is the canvas size in pixels along Y |
Example:
TCanvas* c1 = new TCanvas("c1","test canvas",200,100);
第三类构造函数。
TCanvas::TCanvas ( const char * name,
const char * title = "",
Int_t form = 1
)
form是预定义的画布形式,其定义的值如下:
Create a new canvas with a predefined size form.
If form < 0 the menubar is not shown.
- form = 1 700x500 at 10,10 (set by TStyle::SetCanvasDefH,W,X,Y)
- form = 2 500x500 at 20,20
- form = 3 500x500 at 30,30
- form = 4 500x500 at 40,40
- form = 5 500x500 at 50,50
三、常用方法
1、分割画布和调用画板
(1)分割画布
void TPad::Divide ( Int_t nx = 1,
Int_t ny = 1,
Float_t xmargin = 0.01,
Float_t ymargin = 0.01,
Int_t color = 0
)
方法继承自TPad。通常只使用前两个参数。
Example:
c1->Divide(2,1);// create 2 pads (2 divisions along x, 1 along y).
(2)调用画板
TVirtualPad * TCanvas::cd ( Int_t subpadnumber = 0 )
Example:
c1->cd(2);//from top to bottom, from left to right, is 1,2,3....n
从上到下,从左到右的编号依次为1,2,3.……n。进入对应画板后可以调用Draw()方法进行画图,即可把多个图画在一张画布上。
(3)保存为图片格式
void TPad::SaveAs ( const char * filename = "",
Option_t * option = ""
) const
Example:
c1->SaveAs("figure.png");
最常用的保存格式为pdf、png、jpg、jpeg、gif、root、cpp/cxx/C几类。
if filename is empty, the file produced is padname.ps
if filename starts with a dot, the padname is added in front
if filename ends with .ps, a Postscript file is produced
if filename ends with .eps, an Encapsulated Postscript file is produced
if filename ends with .pdf, a PDF file is produced NOTE: TMathText will be converted to TLatex; q.e.d., symbols only available in TMathText will not render properly.
if filename ends with .svg, a SVG file is produced
if filename ends with .tex, a TeX file is produced
if filename ends with .gif, a GIF file is produced
if filename ends with .gif+NN, an animated GIF file is produced See comments in TASImage::WriteImage for meaning of NN and other .gif sufix variants
if filename ends with .xpm, a XPM file is produced
if filename ends with .png, a PNG file is produced
if filename ends with .bmp, a BMP file is produced
if filename ends with .jpg or .jpeg a JPEG file is produced NOTE: JPEG's lossy compression will make all sharp edges fuzzy.
if filename ends with .tiff, a TIFF file is produced
if filename ends with .C, .cxx,.cpp or .cc, a C++ macro file is produced
if filename ends with .root, a Root file is produced
if filename ends with .xml, a XML file is produced
if filename ends with .json, a JSON file is produced
【资料】
1、ROOT官网——ROOT: analyzing petabytes of data, scientifically. - ROOT
2、ROOT文档——ROOT: TCanvas Class Reference
后续还会不断更新,如有错误请指正。