# Set User range on TGraph not working for x-axis , but OK for y-axis

**URL:** <https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668>\
**Category:** Newbie\
**Created:** [September 1, 2024, 9:17pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668 "2024-09-01T21:17:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [September 1, 2024, 9:17pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/1 "2024-09-01T21:17:13Z")

</div>

I am using ROOT version 6.30/08 on Ubuntu 20.  
I draw several objects on a canvas (TGraph for data plus others for ellipse and lines). I want to set my own drawing limits using SetRangeUser. While the y axis scales correctly, the x-axis does not use the desired limits. I have read the connected topics in the forum, but I am not able the get it right. Here is my python code, which I execute via “pyroot mycode,py”

#define my limits for the plot  
xlow = 120  
xhigh = 220  
ylow = 90  
yhigh = 190

# Create a ROOT TCanvas

c3 = ROOT.TCanvas(“c3”, “PCA Scatter Plot”, 2000, 1500)  
c3.DrawFrame(xlow, ylow, xhigh, yhigh);  
graphl = ROOT.TGraph(lenl, x\_data, y\_data)  
graphl.SetTitle(“PCA components 1 vs 2”)  
graphl.SetMarkerSize(msiz)  
graphl.SetMarkerColor(4)  
graphl.SetMarkerStyle(20)

# Set titles and ranges for X and Y axes

graphl.GetXaxis().SetTitle(“Principal Component 1”)  
graphl.GetYaxis().SetTitle(“Principal Component 2”)  
graphl.Draw(“AP”)  
graphl.GetXaxis().SetRangeUser(xlow, xhigh)  
graphl.GetYaxis().SetRangeUser(ylow, yhigh)

# Draw the error ellipse

ellipse\_data.Draw(“L SAME”)  
linex.Draw(“L SAME”)  
liney.Draw(“L SAME”)  
c3.SetGrid()  
c3.Update()

All objects are drawn, but the x-scale is chosen by ROOT, ignoring my input.

---

<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:** [September 2, 2024, 5:48am UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/2 "2024-09-02T05:48:58Z")

</div>

Probably your limits are larger than the values in TGraph, and SetRangeUser cannot go “outside” those values. In that case, try with SetLimits instead (note that this is ok for TGraph, not so much for histograms). See

> [@How to set ranges on axis?](https://root-forum.cern.ch/t/how-to-set-ranges-on-axis/28254):
>
> The viewing range for a histogram axis can be set from bin ifirst to ilast using the method TAxis::SetRange(ifirst, ilast). Example (hpxpy is a 2D histogram): { TFile f("hsimple.root"); hpxpy-\>Draw("colz"); hpxpy-\>GetYaxis()-\>SetRange(22,23); hpxpy-\>GetXaxis()-\>SetRange(18,19); } Sometimes it is more convenient to set this range using “User’s Coordinates” (axis values) rather than bin numbers. To do that one should use TAxis::SetRangeUser(ufirst, ulast). This method computes the cl…

and

> [@Draw TH1D for larger range](https://root-forum.cern.ch/t/draw-th1d-for-larger-range/55800):
>
> I have a th1d whose range is -10,10 but i want to plot -15,15 SetRangeUser(-15,15) draw the th1d for -10,10 any solution ? ROOT Version: 6.24.00 (conda\_forge) Platform: ubuntu20.04 (wsl) Compiler: gcc9

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [September 2, 2024, 6:55pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/3 "2024-09-02T18:55:05Z")

</div>

Dear Dastudillo,

indeed I like to set limits which are smaller/larger than the values of the data in TGraph. This is true for both the x and y axis. In addition I am drawing an ellipse defining a confidence interval (from a PCA analysis). Some regions of the ellipse indeed go beyond the data point from TGraph, so I want to use my own plotting intervals.

The problem was:

- using -\>GetXaxis()-\>SetRangeUser(xlow, xhigh) and -\>GetYaxis()-\>SetRangeUser(ylow, yhigh) sets the correct limits for the y axis but not for the x axis

---

<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:** [September 3, 2024, 7:12am UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/4 "2024-09-03T07:12:16Z")

</div>

Can you provide a small piece of code we can run showing the problem ?

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [September 3, 2024, 9:04am UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/5 "2024-09-03T09:04:27Z")

</div>

Dear Danilo,

yes, I will provide a stripped down sample code around noon. It will contain also two data files, one for the PCA data, and one for the confidence ellipse. I hope I can append these two files.

Best

Christian

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [September 3, 2024, 9:19am UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/6 "2024-09-03T09:19:17Z")

</div>

I see you are using `c3.DrawFrame(xlow, ylow, xhigh, yhigh)`.  
This should draw frame with necessary axes ranges.  
After this you should draw graph as `graphl.Draw(“P”)` - excluding “A” from the option.

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [September 3, 2024, 11:19am UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/7 "2024-09-03T11:19:20Z")

</div>

Hello Danilo,

if I use the DrawFrame option only (+ “A” instead of “AP”), it will not  
display all the graphic elements, e.g. the axes and data points. The  
ellipse is displayed.  
Appended you find my code and the two files for data and ellipse. With  
the present ranges (no DataFrame and only SetRange) it displays wrong x  
and y scales.  
I tested that with the option “SetRangeUser” and “SetLimits” all is fine.

Thanks for further comments,

best

Christian

(Attachment ErrEllipse.json is missing)

> **(attachments)**
>
> [pca\_Test.pdf](https://root-forum.cern.ch/uploads/short-url/ofYlujyae2YLyuQ7p6h39l4bGU5.pdf) (16.5 KB)  
> [DPCAtest.py](https://root-forum.cern.ch/uploads/short-url/cYjqfadLK0O6Krx7UgFVGFHlGBu.py) (4.58 KB)  
> [PCAData.txt](https://root-forum.cern.ch/uploads/short-url/5ZJk5KQ4Djq0LbhDJ74RHrGYVaP.txt) (13.2 KB)

---

<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:** [September 3, 2024, 1:40pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/8 "2024-09-03T13:40:26Z")

</div>

You did not do what was suggested (plot the frame and then the graph without “A”). Cleaning up and reducing your script:

```auto
#-------------------------------------------------------------------------------
msiz = 1.3 # 0.4 / 1.3
bsiz = 4.4 # 1.65 / 4.4
#------------------------------------------------------------------------------

xlow = 120
xhigh = 220
ylow = 90
yhigh = 190
zlow = 340
zhigh = 420

x_data, y_data, z_data = array('d'), array('d'), array('d')

with open('PCAData.txt', mode='r', encoding='iso-8859-1') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        #print("input : ", row)
        #print(" row[0]: ", row[0]," row[1]: ", row[1]," row[2]: ", row[2], )
        x_data.append(float(row[0]))
        y_data.append(float(row[1]))
        z_data.append(float(row[2]))

lenl = len(x_data)
print(" length of data array : ", lenl)

c3 = ROOT.TCanvas("c3", "PCA Scatter Plot", 200, 10, 800, 600)
c3.DrawFrame(xlow, ylow, xhigh, yhigh)

graphl = ROOT.TGraph(lenl)
for i in range(lenl):
    graphl.SetPoint(i, x_data[i], y_data[i])

graphl.Draw("P") # "A" for axes, "L" for lines, "P" for markers
graphl.SetMarkerSize(msiz)
graphl.SetMarkerColor(4)
graphl.SetMarkerStyle(20)
graphl.SetTitle("PCA components 1 vs 2, Llamas")

graphl.GetXaxis().SetTitle("Principal Component 1")
graphl.GetYaxis().SetTitle("Principal Component 2")

# Ellipse
el1 = ROOT.TEllipse(145,175,25,40)
el1.Draw() # no need for "L SAME"

c3.SetGrid()
c3.Update()

# Keep the canvas open
input("Press Enter to exit...")

```

gives this, as wanted:

 ![c3](https://root-forum.cern.ch/uploads/default/original/3X/3/2/32d24ddd784f250cf467a5027ee9bd62b622d418.png)

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [September 3, 2024, 8:35pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/9 "2024-09-03T20:35:32Z")

</div>

Dear Danilo,

sorry, I just interchanged “A” and “P” in the “graphl.Draw(” ") statement. When I do it right, the display ranges come out correctly, using only the statement “DrawFrame(xlow,ylow,xhigh,yhigh)” and not “SetRange” etc. The trick then really is to not allow the “A” (=axis) as an argument in graphl.Draw() and only supply “P” for the data points. An elegant solution, which makes perfect sense, but not easy to guess for the casual user.

Thank you very much for your support!

Best

Christian

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [September 5, 2024, 12:32pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/10 "2024-09-05T12:32:44Z")

</div>

Dear Danilo,

sorry to bug you again in this matter: With the omission of “A” in the statement graphl(“P”) the scaling is perfect, but the title and axis labels are no more printed (I took exactly your code below). Any suggestion?

Thanks and best wishes.

Christian

---

<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:** [September 5, 2024, 3:47pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/11 "2024-09-05T15:47:07Z")

</div>

> [@Problem in setting the range of the plot](https://root-forum.cern.ch/t/problem-in-setting-the-range-of-the-plot/50089/10):
>
> Actually, for “graphs”, it is very easy to use any “arbitrary values” for the x-axis range: graph-\>GetHistogram()-\>GetXaxis()-\>SetLimits(xmin, xmax);

> [@Set first axis number to draw](https://root-forum.cern.ch/t/set-first-axis-number-to-draw/50912/6):
>
> Try: mg\_comp\_Stz\_Q2-\>Draw("ALP"); gPad-\>Modified(); gPad-\>Update(); mg\_comp\_Stz\_Q2-\>GetHistogram()-\>GetXaxis()-\>SetLimits(0.5, 69.5); // or maybe (1., 70.) gPad-\>Modified(); gPad-\>Update();

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [September 5, 2024, 4:01pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/12 "2024-09-05T16:01:11Z")

</div>

> [@kiesling](#):
>
> sorry to bug you again in this matter: With the omission of “A” in the statement graphl(“P”) the scaling is perfect, but the title and axis labels are no more printed (I took exactly your code below). Any suggestion?

To draw titles, do:

```auto
h = c3.DrawFrame(xlow, ylow, xhigh, yhigh, "graph title")
h.GetXaxis().SetTitle("Principal Component 1")
h.GetYaxis().SetTitle("Principal Component 2")

```

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [September 5, 2024, 5:06pm UTC](https://root-forum.cern.ch/t/set-user-range-on-tgraph-not-working-for-x-axis-but-ok-for-y-axis/60668/13 "2024-09-05T17:06:56Z")

</div>

Thank you very much, it works !
