Xyplot

Example showing how to produce a plot with an orthogonal axis system centered at (0,0).

Author: Olivier Couet
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Thursday, May 19, 2022 at 08:19 AM.

In [1]:
TCanvas *c = new TCanvas("c","XY plot",200,10,700,500);

Remove the frame

In [2]:
c->SetFillColor(kWhite);
c->SetFrameLineColor(kWhite);
c->SetFrameBorderMode(0);

Define and draw a curve the frame

In [3]:
const Int_t n = 4;
Double_t x[n] = {-1, -3, -9, 3};
Double_t y[n] = {-1000,  900,  300, 300};
TGraph* gr = new TGraph(n,x,y);
gr->SetTitle("XY plot");
gr->SetMinimum(-1080);
gr->SetMaximum(1080);
gr->SetLineColor(kRed);
gr->Draw("AC*");

Remove the frame's axis

In [4]:
gr->GetHistogram()->GetYaxis()->SetTickLength(0);
gr->GetHistogram()->GetXaxis()->SetTickLength(0);
gr->GetHistogram()->GetYaxis()->SetLabelSize(0);
gr->GetHistogram()->GetXaxis()->SetLabelSize(0);
gr->GetHistogram()->GetXaxis()->SetAxisColor(0);
gr->GetHistogram()->GetYaxis()->SetAxisColor(0);

gPad->Update();

Draw orthogonal axis system centered at (0,0). Draw the Y axis. Note the 4th label is erased with SetLabelAttributes

In [5]:
TGaxis *yaxis = new TGaxis(0, gPad->GetUymin(),
                           0, gPad->GetUymax(),
                           gPad->GetUymin(),gPad->GetUymax(),6,"+LN");
yaxis->ChangeLabel(4,-1,0.);
yaxis->Draw();

Draw the y-axis title.

In [6]:
TLatex *ytitle = new TLatex(-0.5,gPad->GetUymax(),"Y axis");
ytitle->Draw();
ytitle->SetTextSize(0.03);
ytitle->SetTextAngle(90.);
ytitle->SetTextAlign(31);

Draw the x axis

In [7]:
TGaxis *xaxis = new TGaxis(gPad->GetUxmin(), 0,
                           gPad->GetUxmax(), 0,
                           gPad->GetUxmin(),gPad->GetUxmax(),510,"+L");
xaxis->Draw();

Draw the x axis title.

In [8]:
TLatex *xtitle = new TLatex(gPad->GetUxmax(),-200.,"X axis");
xtitle->Draw();
xtitle->SetTextAlign(31);
xtitle->SetTextSize(0.03);

Draw all canvases

In [9]:
gROOT->GetListOfCanvases()->Draw()