Drawing Histograms with for loop with python?

My code is set up such that I have a dictionary of histograms (“trigger_mass_hist”), with each key being the name of a trigger and each value being the histogram itself. I want to graph all of the histograms in the directory on one canvas (maybe 10 histograms), but when I try to graph them as follows:

canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
for hist in trigger_mass_hist:
    trigger_mass_hist[hist].Draw("hist e1")
    canv1.Update()

only the last histogram shows up on the canvas. Any advice?

Hi,

here the idea is to add to the drawing options the string “same” for all histograms after the first one.

canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts="hist e1"
for hist in trigger_mass_hist:
    trigger_mass_hist[hist].Draw(draw_opts)
    draw_opts += " same"
    canv1.Update()

Cheers,
Danilo

Hi,

But then the 4th histogram will have `“hist e1 same same same” - which breaks my C++, performance-focused heart…

:wink:

Axel.

A hackish way would be doing it only if not present:

canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts = "hist e1"
for hist in trigger_mass_hist:
    trigger_mass_hist[hist].Draw(draw_opts)
    if "same" not in draw_opts:
        draw_opts += " same"
    canv1.Update()

But this will be slow (in is O(n)). Better do it with enumerate:

canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts = "hist e1"
for i, hist in enumerate(trigger_mass_hist):
    trigger_mass_hist[hist].Draw(draw_opts)
    if i == 0:
        draw_opts += " same"
    canv1.Update()

Or, the third option, use a THStack:

canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts = "hist e1"
stack = ROOT.THStack()
for hist in trigger_mass_hist.values():
    stack.Add(hist)
stack.Draw("nostack" + draw_opts)
canv1.Update()
1 Like

I draw one of them before the loop and then use the ‘same’ option in loop

from rootpy.interactive import wait
#add option or what ever
trigger_mass_hist.pop().Draw(draw_opts)
for hist in trigger_mass_hist:
      hist.Draw('same'+draw_opts)
wait()

This works for me
Cheers
Daniel

Hi,

of course there was a flaw in the solution I proposed :slight_smile: Anything like the previous very nice solutions of course would work!
I’d like at least to add another one to cope with the glitch above !

canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts="hist e1"

for i, hist in enumerate(trigger_mass_hist.itervalues()):
    hist.Draw(draw_opts + "" if i==0 else " same")
    canv1.Update()

which avoids the lookup in the dictionary.

Cheers,
Danilo

Many of these solutions work perfectly! I ended up using the second one proposed by @Graipher. Thank you!