Putting the axes on "0" value

Hello,

In ROOT, the axes are always set at the border of the pad, as a bounding box. This is fine for most cases, but in one case I would like to have the axes centered on the “0” value and not in the border, like the attached example.

How could I do that?

Thanks
Dominique


You will have to use TGaxis.
I will send you an example.

here is the idea:

{
   TCanvas *c = new TCanvas("c","c",0,0,500,500);
   c->Draw();
   c->Range(-10,-10,10,10);   

   TF1 *f = new TF1("f","x*x",-10,10);
   f->Draw("same");

   ox = new TGaxis(-10,0,10,0,-10,10,510,"");           
   ox->Draw();
   oy = new TGaxis(0,-10,0,10,-10,10,510,"");
   oy->Draw();
}

Thanks, this works, but it’s not very convenient: many options like SetGridx() etc. are disabled, and it is not so simple to manipulate the Canvas. Isn’t there a way to create a style such that I would call it via something like:

gROOT->SetStyle("mystyle"); TF1 *f=new TF1("f","x*x",-1,1); f->Draw();

I.e., can I in some way modify the drawing style such that the axis would be set to “0” when I use this style? This would be much more convenient.

Thanks
Dominique

I was just an idea … now to make it look nicer we should play with the TGaxis options. The style cannot act on this kind of plot, because it is done using low level functionalities outside the style scope. Do you want me to work a bit more on the macro to make it look nicer or are you willing to do it yourself ?

Hi,

Can you please show me how we could make it nicer? In particular, how to add a grid?

Thank you
Dominique

{
   TCanvas *c = new TCanvas("c","c",0,0,500,500);
   c->Draw();
   c->Range(-11,-11,11,11);   

   TF1 *f = new TF1("f","x*x",-10,10);
   f->SetLineWidth(1); 
   f->SetLineColor(kRed);
   f->Draw("same");

   TGaxis *ox = new TGaxis(-10,0,10,0,-10,10,510,"+-S"); 
   ox->SetTickSize(0.009);
   ox->SetLabelFont(42);
   ox->SetLabelSize(0.025);
   ox->Draw();
   TGaxis *oxg = new TGaxis(-10,-10,10,-10,-10,10,510,"SWB");
   oxg->SetLabelSize(0.);   
   oxg->SetTickSize(0.);   
   oxg->SetGridLength(0.9);
   oxg->Draw();

   TGaxis *oy = new TGaxis(0,-10,0,10,-10,10,510,"+-S"); 
   oy->SetTickSize(0.009);
   oy->SetLabelFont(42);
   oy->SetLabelSize(0.025);
   oy->Draw();
   TGaxis *oyg = new TGaxis(-10,-10,-10,10,-10,10,510,"SWB"); 
   oyg->SetLabelSize(0.);
   oyg->SetTickSize(0.);
   oyg->SetGridLength(0.9);
   oyg->Draw();
}

Thanks a lot, this looks very nice. I’ll make a macro with it.

Dominique