# How to change the position of axes of a graph

**URL:** <https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970>\
**Category:** Newbie\
**Tags:** root\
**Created:** [October 14, 2022, 9:04pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970 "2022-10-14T21:04:07Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![BBS](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bbs/32/31208_2.png) [@BBS](https://root-forum.cern.ch/u/BBS)\
**Post date:** [October 14, 2022, 9:04pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/1 "2022-10-14T21:04:07Z")

</div>

Hello everyone,

I’m trying to make a graph and I want it to go below the X axis. Unfortunately, it seems that root automatically arranges the axes so that the graph is always contained inside the box. Here is an image of my graph:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/c/b/cb884b17a1af581ed959d0b0e9060fc18500ba60.png)  
I want it so that X axis start at Y=0 and vice versa. Of course, Y axis should go below too. How can I achieve this?

Example:  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/3/0/304d8c0f5e5046ec4b5507fe4a389ea556b2728f.png)  
©Erhan Gülmez, Bogazici University, Istanbul, Turkey

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 15, 2022, 4:49am UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/2 "2022-10-15T04:49:31Z")

</div>

> [@Problem in setting the range of the plot](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089):
>
> Dear Root experts, I have a problem on setting the range of a TGraphErrors plot. The comands that I wrote in the macro are the following: g-\>GetXaxis()-\>SetTitle("E (MeV)"); g-\>GetXaxis()-\>SetRangeUser(1e-9,10.); g-\>GetYaxis()-\>SetTitle("Estimated/Ref"); g-\>GetYaxis()-\>SetRangeUser(0.,4.5); gPad-\>Modified(); gPad-\>Update(); For the y-axis no problem. However, for the x-axis it do not change whatever values I put in the comand GetXaxis. I am writing something wrong in the co…

---

<div class="post-metadata">

**Author:** ![BBS](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bbs/32/31208_2.png) [@BBS](https://root-forum.cern.ch/u/BBS)\
**Post date:** [October 15, 2022, 1:14pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/3 "2022-10-15T13:14:42Z")

</div>

I’ve read that topic before posting and either I’m missing something or it just changes the ranges of the axes. If i’m missing something would you point out what it is? I’ve also added the graph visual that I’m trying to get.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 15, 2022, 2:30pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/4 "2022-10-15T14:30:11Z")

</div>

[TGaxis](https://root.cern/doc/master/classTGaxis.html)

---

<div class="post-metadata">

**Author:** ![BBS](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bbs/32/31208_2.png) [@BBS](https://root-forum.cern.ch/u/BBS)\
**Post date:** [October 15, 2022, 2:33pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/5 "2022-10-15T14:33:17Z")

</div>

I’ve tried that too and could set another axis at my preferred position but couldn’t get rid of the original axis.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 15, 2022, 2:53pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/7 "2022-10-15T14:53:29Z")

</div>

There are examples in the [TGaxis](https://root.cern/doc/master/classTGaxis.html) class reference that show how one can use the [TPad::Range](https://root.cern/doc/master/classTPad.html#a608b9c0c673c66bb5c8946955ec11098) to set a world coordinate system for a pad (search for “`Range`”).

Try also: `grep -r '>Range(' ${ROOTSYS}/t[eu]*`

---

<div class="post-metadata">

**Author:** ![BBS](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bbs/32/31208_2.png) [@BBS](https://root-forum.cern.ch/u/BBS)\
**Post date:** [October 15, 2022, 4:48pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/8 "2022-10-15T16:48:47Z")

</div>

I couldn’t figure it out from there either but I haven’t given up yet 🙂  
Thanks for your time.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 16, 2022, 9:26am UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/14 "2022-10-16T09:26:04Z")

</div>

Maybe @couet has a better idea.

```cpp
{
  // create the graph
  Double_t x[3] = {0., 400., 2700.};
  Double_t y[3] = {2.3, -0.1, -0.4};
  TGraph *g = new TGraph(3, x, y);
  Double_t xmin = -300., xmax = 3100.; // x-axis range
  Double_t ymin = -0.7, ymax = 2.6; // y-axis range
  // create and draw the canvas
  TCanvas *c = new TCanvas("c", "c");
  Double_t r = Double_t(c->GetWw()) / Double_t(c->GetWh()); // aspect ratio
  Double_t ml = 0.05; // (bottom, top, left, right) margin length (percentage)
  Double_t al = 0.04; // arrow length (percentage of the pad height)
  Double_t dx = (xmax - xmin) / (1. - 2. * ml - al / r);
  Double_t dy = (ymax - ymin) / (1. - 2. * ml - al);
  c->Range(xmin - dx * ml, ymin - dy * ml,
           xmax + dx * (ml + al / r), ymax + dy * (ml + al));
  // draw the graph
  g->Draw("L*");
  // create and draw the x-axis
  TGaxis *ax = new TGaxis(xmin, 0., xmax, 0., xmin, xmax, 510, "-+S");
  ax->SetTickLength(al * r / 4.);
  ax->ChangeLabel(1, -1, -1, -1, -1, -1, " "); // remove "0"
  ax->SetLabelOffset(-0.075);
  ax->SetTitle("Retarding Potential");
  ax->SetTitleOffset(-1.5);
  ax->Draw();
  TArrow *axa = new TArrow(xmax, 0., xmax + dx * al / r, 0., al, "|>");
  axa->SetAngle(30);
  axa->Draw();
  // create and draw the y-axis
  TGaxis *ay = new TGaxis(0., ymin, 0., ymax, ymin, ymax, 510, "-+S");
  ay->SetTickLength(al / 4.);
  ay->ChangeLabel(2, -1, -1, -1, -1, -1, " "); // remove "0"
  ay->SetTitle("Photocell current");
  ay->SetTitleOffset(-1.3);
  ay->Draw();
  TArrow *aya = new TArrow(0., ymax, 0., ymax + dy * al, al, "|>");
  aya->SetAngle(30);
  aya->Draw();
}

```

---

<div class="post-metadata">

**Author:** ![BBS](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bbs/32/31208_2.png) [@BBS](https://root-forum.cern.ch/u/BBS)\
**Post date:** [October 16, 2022, 2:13pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/15 "2022-10-16T14:13:09Z")

</div>

Thank you kindly, I had done something like this:

```auto
{
    TGraphErrors *gr = new TGraphErrors("data.txt");

    TCanvas *c1 = new TCanvas();
    c1->SetFrameLineColor(0);

    TGaxis *axis1 = new TGaxis(0, 0, 3000, 0, 0, 3000, 506, "");
    axis1->SetLabelSize(0.03);
    axis1->SetLabelOffset(-0.06);
    axis1->SetLabelFont(42);
    axis1->SetTitle("Retarding Potential(mV)");
    axis1->SetTitleSize(0.036);
    axis1->SetTitleFont(62);
    axis1->SetTickLength(10);
    axis1->ChangeLabel(1,-1,0,-1,-1,-1,-1);

    gr->SetMarkerStyle(kDot);
    
    gr->GetXaxis()->SetRangeUser(0, 3000);
    gr->GetXaxis()->SetAxisColor(0);

    gr->GetYaxis()->SetRangeUser(-0.5, 2.5);
    gr->GetYaxis()->SetTitle("Photocell Current(mA)");
    gr->GetYaxis()->SetTitleSize(0.036);
    gr->GetYaxis()->SetTitleFont(62);
    
    
    gr->Draw("APL");
    axis1->Draw();
}

```

Basically, I set the manually created TGaxis’ and the original one’s ranges the same and then made the original one invisible with SetAxisColor(0). Also did the same for the FrameLineColor(0) for canvas.

I also tried creating the axis first and drawing the graph without the “A” option but wasn’t able to get any visual. Now I realize that I drew only the x-axis, is that why the graph wasn’t shown? If not, where did you “relate” the axes and graph?

Thank you again for taking the time to write this, I’ll learn a lot by analyzing it.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 25, 2022, 11:52am UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/16 "2022-10-25T11:52:40Z")

</div>

Yes that’s the way to proceed. I have the following example in my collection of macro. It is similar.

```auto
{
   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();
}

```

 ![Screenshot 2022-10-25 at 13.52.25](https://root-forum.cern.ch/uploads/default/original/3X/7/3/7311480ece992572097ef94e6c45f808eff93e70.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 25, 2022, 12:07pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/17 "2022-10-25T12:07:29Z")

</div>

@couet Maybe you could extend the [TGaxis](https://root.cern/doc/master/classTGaxis.html) so that nice “arrows” could be added on its ends (i.e., “`<-`”, “`->`”, “`<|-`”, “`-|>`”).

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 25, 2022, 12:44pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/18 "2022-10-25T12:44:54Z")

</div>

Yes It might be simple to do, by importing axis body drawing of the following macro into [Axis body painting](https://github.com/root-project/root/blob/8b5c4d6cb8af23f008284105073be19cefeecc7b/graf2d/graf/src/TGaxis.cxx#L1477)

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

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

   // Draw Axis tick marks and labels
   auto ox = new TGaxis(-10,0,10,0,-10,10,510,"+-SB");
   ox->SetTickSize(0.009);
   ox->SetLabelFont(42);
   ox->SetLabelSize(0.025);
   ox->Draw();
   auto oy = new TGaxis(0,-10,0,10,-10,10,510,"+-SB");
   oy->SetTickSize(0.009);
   oy->SetLabelFont(42);
   oy->SetLabelSize(0.025);
   oy->Draw();

   // Draw axis body with arrow.
   auto oxa = new TArrow(-10,0,10,0);
   oxa->Draw();
   auto oya = new TArrow(0,-10,0,10);
   oya->Draw();

   // Draw grid.
   auto oxg = new TGaxis(-10,-10,10,-10,-10,10,510,"SWB");
   oxg->SetLabelSize(0.);
   oxg->SetTickSize(0.);
   oxg->SetGridLength(0.9);
   oxg->Draw();
   auto oyg = new TGaxis(-10,-10,-10,10,-10,10,510,"SWB");
   oyg->SetLabelSize(0.);
   oyg->SetTickSize(0.);
   oyg->SetGridLength(0.9);
   oyg->Draw();
}

```

 ![Screenshot 2022-10-25 at 14.42.06](https://root-forum.cern.ch/uploads/default/original/3X/d/0/d079c5b45905de9e1c6a259faa61b119b992dca5.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 25, 2022, 1:52pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/19 "2022-10-25T13:52:46Z")

</div>

I guess people would appreciate [something prettier](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/14) (e.g., nice “arrows” could be added outside the axis limits).

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 25, 2022, 2:22pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/20 "2022-10-25T14:22:19Z")

</div>

Yes, I was just pointing the fact the Axis body drawing can be (easily) improved.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 25, 2022, 3:08pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/21 "2022-10-25T15:08:46Z")

</div>

> <https://github.com/root-project/root/issues/11634>
>
> Extend the \[TGaxis\](https://root.cern/doc/master/classTGaxis.html) so that nice …“arrows” could be added on its ends.
> As requested \[here\](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/16):

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 28, 2022, 4:06pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/22 "2022-10-28T16:06:07Z")

</div>

PR → [Arrow on axis by couet · Pull Request #11645 · root-project/root · GitHub](https://github.com/root-project/root/pull/11645)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 3, 2022, 12:33pm UTC](https://root-forum.cern.ch/t/how-to-change-the-position-of-axes-of-a-graph/51970/23 "2022-11-03T12:33:24Z")

</div>

PR is merged.
