# Drawing Vertical Line in TGraph

**URL:** <https://root-forum.cern.ch/t/drawing-vertical-line-in-tgraph/56640>\
**Category:** Newbie\
**Created:** [October 7, 2023, 9:13pm UTC](https://root-forum.cern.ch/t/drawing-vertical-line-in-tgraph/56640 "2023-10-07T21:13:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [October 7, 2023, 9:13pm UTC](https://root-forum.cern.ch/t/drawing-vertical-line-in-tgraph/56640/1 "2023-10-07T21:13:43Z")

</div>

Hi, I am trying to draw veritcal line in my TGraph. My code compiles without error but the line is out of scope (picture attached for reference where you can see red line on bottom part). Can someone please help with the issue? Thank you.

```auto
void code() {
       const Int_t nPoints = 5;
       Double_t x[nPoints] = {1, 2, 3, 4, 5};
       Double_t y[nPoints] = {2.0, 3.5, 1.5, 4.0, 2.5};

       TGraph *graph = new TGraph(nPoints, x, y);

       TCanvas *canvas = new TCanvas("canvas", "Graph Example", 800, 600);

       graph->SetTitle("Example Graph");
       graph->GetXaxis()->SetTitle("X-axis");
       graph->GetYaxis()->SetTitle("Y-axis");

       graph->Draw("APL"); // A: Axis, P: Points, L: Line
        
        TLine *verticalLine = new TLine(2, canvas->GetUymin(), 2, canvas->GetUymax());
        verticalLine->SetLineColor(kRed);
        verticalLine->SetLineWidth(2);

        verticalLine->Draw("same");
       canvas->Draw();
       }

```

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/6/d/6d7573355bf66621f66af0d66bdca690f043c3ea.png)

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 8, 2023, 5:08am UTC](https://root-forum.cern.ch/t/drawing-vertical-line-in-tgraph/56640/2 "2023-10-08T05:08:39Z")

</div>

Try

```auto
  TLine *verticalLine = new TLine(2, graph->GetYaxis()->GetXmin(), 2, graph->GetYaxis()->GetXmax());

```

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [October 8, 2023, 7:14pm UTC](https://root-forum.cern.ch/t/drawing-vertical-line-in-tgraph/56640/3 "2023-10-08T19:14:33Z")

</div>

It worked! But why GetXmin() and GetXmax() instead of GetYmin() and GetYmax()? Thank you

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 9, 2023, 2:10am UTC](https://root-forum.cern.ch/t/drawing-vertical-line-in-tgraph/56640/4 "2023-10-09T02:10:47Z")

</div>

Both [GetXaxis() and GetYaxis()](https://root.cern/doc/master/classTGraph.html#aac9ab3e73670c37276ef656934d20c35) return a TAxis object, which by itself is neither x nor y, but the methods to get the maximum and minimum from a TAxis happen to be called [GetXmax() and GetXmin()](https://root.cern/doc/master/classTAxis.html#a5ae359f6f1fbbb078cd425a383582b11).

---

<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 10, 2023, 8:46am UTC](https://root-forum.cern.ch/t/drawing-vertical-line-in-tgraph/56640/5 "2023-10-10T08:46:28Z")

</div>

simplified code:

```auto
void code() {
   const Int_t nPoints = 5;
   Double_t x[nPoints] = {1, 2, 3, 4, 5};
   Double_t y[nPoints] = {2.0, 3.5, 1.5, 4.0, 2.5};

   auto canvas = new TCanvas("canvas", "Graph Example", 800, 600);

   auto graph = new TGraph(nPoints, x, y);
   graph->SetTitle("Example Graph");
   graph->GetXaxis()->SetTitle("X-axis");
   graph->GetYaxis()->SetTitle("Y-axis");

   graph->Draw("AL");

   TLine *verticalLine = new TLine(2, graph->GetYaxis()->GetXmin(),
                                   2, graph->GetYaxis()->GetXmax());
   verticalLine->SetLineColor(kRed);
   verticalLine->SetLineWidth(2);

   verticalLine->Draw();
}

```
