Filling a divided canvas with multiple TGraphs from within a for loop

Hi All,

I am trying to get a TGraphErrors plot in each of the sub-divisions of the canvas for comparison. Unfortunately, I get only the last plot in the last subdivision after saving the canvas in a root file. I can see all the plots getting plotted at their destinations while the for loop runs, but at the end only one segment has the plot. Code snippet and canvas image attached below:

 ...
 ck = TCanvas('ck', 'canvas', 800, 800)
 ck.Divide(4,4, 0.001, 0.001)

 for k in range(1, 17):
    arr = np.asarray(p[k-1]).astype(float)      #p = [[] for i in range(16)] populated from a txt file
    volt 	= array('d', arr[:, 1])
    Gain 	= array('d', arr[:, 4])
    Gain_e 	= array('d', arr[:, 5])

    ck.cd(k)
    gr2 = TGraphErrors(70, volt, Gain, zero, Gain_e) #zero is array of zeroes
    gr2.Draw()
    ck.Update()    
    ck.Modified() 

 ck.Write()

What am I doing wrong or is this a wrong approach?

Try something like this:

    pad = ck.cd(k)
    gr2 = TGraphErrors(70, volt, Gain, zero, Gain_e) #zero is array of zeroes
    gr2.Draw()
    pad.Modified() 
    pad.Update()    

or:

    ck.cd(k)
    gr2 = TGraphErrors(70, volt, Gain, zero, Gain_e) #zero is array of zeroes
    gr2.Draw()
    gPad.Modified() 
    gPad.Update()    

Tried both. Still the same

Try:

    gr2 = TGraphErrors(70, volt, Gain, zero, Gain_e) #zero is array of zeroes
    pad = ck.cd(k)
    gROOT.SetSelectedPad(pad)
    gr2.DrawClone()
    pad.Modified()
    pad.Update()

and then:

ck.cd(0)
ck.Write()
1 Like
    gr2 = TGraphErrors(70, volt, Gain, zero, Gain_e) #zero is array of zeroes
    pad = ck.cd(k)
    gROOT.SetSelectedPad(pad)
    gr2.DrawClone()                 #gr2.Draw()
    pad.Modified()
    pad.Update()

I get a blank canvas when I put DrawClone() instead of Draw(). Other than that no changes.

I can see each instance of for loop plot a pad with the correct data but rest of them are blank. As the loop progresses the previously drawn pads go blank as well. Last instance of for loop leaves me with the 16th pad plotted. For some reason the previously plotted pads aren’t getting retained with the loop.

Try:

Thanks for the help @Wile_E_Coyote and @bellenot

But I may have figured out a solution. I believe the same variable ‘gr1’ for all instances of the loop is causing this problem as it wasn’t getting re-initialized. I made a list of them and it worked. Code and image attached below:

gr = ['gr1', 'gr2', 'gr3', 'gr4', 'gr5', 'gr6', 'gr7', 'gr8', 'gr9',
 'gr10', 'gr11', 'gr12', 'gr13', 'gr14', 'gr15', 'gr16']

for k in range(1, 17):
    arr = np.asarray(p[k-1]).astype(float)      #p = [[] for i in range(16)] populated from a txt file
    volt 	= array('d', arr[:, 1])
    Gain 	= array('d', arr[:, 4])
    Gain_e 	= array('d', arr[:, 5])

    gr[k-1] = TGraphErrors(70, volt, Gain, zero, Gain_e) #zero is array of zeroes
    pad = ck.cd(k)
    gROOT.SetSelectedPad(pad)
    gr[k-1].Draw()
    pad.Modified()
    pad.Update()

ck.cd()
ck.Write()

1 Like