What is the correct way of THStack.GetXaxis().SetRangeUser() in for loop?

I was doing THstack plots in a for loop, where the histograms h1,h2,h3 are drawn from RDataFrame. After the loop, I want to do some fine tuning on some plots.
This is the working part of my code:

c1 = r.TCanvas("c1","c1 title",1500,1200)
c1.Divide(3,3)

h1 = df.Histo1D("var1")
h2 = df.Histo1D("var2")
h3 = df.Histo1D("var3")

hs1_list = [] # save objects in the for loop, to ease restyle later
for i in range(9):
    c1.cd(i+1)
    varname = varnames[i]
    hs = r.THStack("hs1{}".format(i+1),par_list[i][0])
    hs1_list.append(hs)
    hs.Add(h1.GetPtr())
    hs.Add(h2.GetPtr())
    hs.Add(h3.GetPtr())
    hs.DrawClone("nostack HIST E")

# fine tune some plots
hs1_list[0].GetXaxis().SetRangeUser(0,3100)

c1.Draw()

The hs1_list[0].GetXaxis().SetRangeUser(0,3100) line throw an error:
ReferenceError: attempt to access a null-pointer
sorry for not being able to include the full code.
Could anyone help me on that ?


_ROOT Version: 6.24 (PyROOT via conda)
_Platform:Centos7
_Compiler: gcc9


hs1_list[0] seems not initialised. I suppose hs1_list.append(hs) should have done it ? it does not seem to be the case.
May be @etejedor can help.

I wrote in Python, so the hs1_list should be initialized automatically. I use hs1_list to store 9 (pointers? i think) of the THStack generated in the for loop. I tested to change the Xaxis range only for the 1st THStack (hs1_list[0]) before drawing the whole canvas.

Try to add a gPad->Update() (that’s the C++ syntax, make it in Python) before the the GetAxis() line.

Sorry, it didnt work, the same error still being thrown.

I tried use Draw() in stead of DrawClone() in the for loop, and it worked.
But in the previous similar plots, DrawClone() must be used otherwise it only plot the last plot in the ```for`` loop. Surprisingly, it worked in this case.

Yes Draw is better. DrawClone draws a copy of the stack so the original stack is not drawn.