Drawing two added TGraphs while looping over several variables

Hi all,
I am trying to add two TGraphs for each variable I loop over. I tried the following:

c=ROOT.TCanvas('c','c',500,500)
for var in variables:
     
        ['tgraph1_%s'%var].Draw('apc')
	['tgraph2_%s'%var].Draw('SAME')

        c.cd()
	c.Modified()
	c.Update()
	c.SaveAs('test.png')

However, this adds all the graphs together, whereas I only want one graph (with two lines) for each variable. How can I do that?

Cheers

I am not sure I fully understand you macro. In the loop you first draw a graph redefining the axes (option “a”) and then a graph on top (by the way "same: is not a graph option use “pc” instead). so at te end of the loop you should see only two graphs.

1 Like

Thanks! This seems to work, but it seemst to adapt the scale to the first TGraph, so that the second one is not shown. How can I manually change the range?

You need to use the class TMultiGraph

I tried that before, but If I do

        mg.Add( ['tgraph1_%s'%var])
	mg.Add( ['tgraph2_%s'%var])
	mg.Draw('apc')

	c.cd()
	c.Modified()
	c.Update()
	
	

It again adds up every graph of each variable in one plot…

Just put the graphs you need inside the multigraph and it will plot only those. I think you should revisit the logic of your loop.

Right, so there is no way to just “clear” the multigraph?

It would be better to make sure it is properly filled. But, yes there is a way:

mg->GetListOfGraphs()->Clear();
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.