SetDecimals() in TGaxis not working

Dear Rooters,

I would like that the labels in my axes had a fix precision, the same for both axes. For example, if the labels are 0.9, 1, 1.1 I’d like that they were 0.9, 1.0, 1.1 instead.

MWE.pdf (12.6 KB)

For some TMultiGraph mg I can do mg->GetXaxis()->SetDecimals(1) which solves this.
However, for a “self-made” TGaxis, the command SetDecimals(1) doesn’t do the job. It is also not responding to ChangeLabel(). I tried both commands before and after the DrawAxis().

Here’s a minimal working example reproducing the problem:

int MWE()
{  
  gStyle->SetOptFit ( 1011 );
  gStyle->SetTextFont(132);
  gStyle->SetTextSize(0.06);
  gStyle->SetTitleFont(132,"x");
  gStyle->SetTitleFont(132,"y");
  gStyle->SetTitleFont(132,"z");
  gStyle->SetLabelFont(132,"x");
  gStyle->SetLabelFont(132,"y");
  gStyle->SetLabelFont(132,"z");
  gStyle->SetLabelSize(0.05,"x");
  gStyle->SetTitleSize(0.06,"x");
  gStyle->SetLabelSize(0.05,"y");
  gStyle->SetTitleSize(0.06,"y");
  gStyle->SetLabelSize(0.05,"z");
  gStyle->SetTitleSize(0.06,"z");
  
  gStyle->SetTickLength(0.03,"x");
  gStyle->SetTickLength(0.03,"y");
  gStyle->SetTickLength(0.03,"z");
  
  
  gStyle->SetLegendBorderSize(1);
  gStyle->SetLegendFillColor(0);
  gStyle->SetLegendFont(132);
  gStyle->SetLegendTextSize(0.05);
  // use bold lines and markers
  gStyle->SetMarkerStyle(8);
  gStyle->SetHistLineWidth(1);//1.85);
  gStyle->SetLineStyleString(2,"[12 12]"); // postscript dashes
  // do not display any of the standard histogram decorations
  gStyle->SetOptTitle(1);
  gStyle->SetOptStat(1);
  gStyle->SetOptFit(1);
  // put tick marks on top and RHS of plots
  gStyle->SetPadTickX(1);
  gStyle->SetPadTickY(1);
  //R.K. abool clumsy axis lables
  gStyle->SetNdivisions(509); // default root value is 510
  
  gStyle->SetEndErrorSize(8);
  
 TCanvas *c=new TCanvas("c","MWE",800,350);

  c->Range(0.8, -1, 2.3, 0);

  TGaxis *axis = new TGaxis();
  axis->SetName("axis");
  axis->SetTickLength(0.06);
  axis->SetLabelSize(0.06);
  axis->SetTextFont(132);
  axis->SetLabelFont(132);
  axis->DrawAxis(0.9,-0.5,2.2,-0.5,0.9,2.2,510,"S+");
  axis->SetDecimals(1);
  axis->ChangeLabel(5,-1,-1,-1,-1,-1,"bla");
  c->Update(); 
  return 0;
}

I tried setting globally:

TAxis::SetDecimals(1);
TGaxis::SetDecimals(1);
TAttAxis::SetMaxDigits(2);

but I get:

error: call to non-static member function without an object argument
  TAxis::SetDecimals(1);

and similar errors for the other two.


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.14/04
_Platform: Ubuntu 18.04.2 LTS (bionic)
_Compiler: ROOT CINT


{  
	TCanvas *c=new TCanvas("c","MWE",800,350);
	c->Range(0.8, -1, 2.3, 0);
	TGaxis *axis = new TGaxis(0.9,-0.5,2.2,-0.5,0.9,2.2,510,"S+");
	axis->SetName("axis");
	axis->SetTickLength(0.06);
	axis->SetLabelSize(0.06);
	axis->SetTextFont(132);
	axis->SetLabelFont(132);
	axis->Draw();
	axis->SetDecimals(1);
	axis->ChangeLabel(5,-1,-1,-1,-1,-1,"bla");
}

1 Like

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