# Plot an overlay histogram from multiple .txt files

**URL:** <https://root-forum.cern.ch/t/plot-an-overlay-histogram-from-multiple-txt-files/53813>\
**Category:** Newbie\
**Tags:** python, hist, root\
**Created:** [February 28, 2023, 9:50am UTC](https://root-forum.cern.ch/t/plot-an-overlay-histogram-from-multiple-txt-files/53813 "2023-02-28T09:50:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdash](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@gdash](https://root-forum.cern.ch/u/gdash)\
**Post date:** [February 28, 2023, 9:50am UTC](https://root-forum.cern.ch/t/plot-an-overlay-histogram-from-multiple-txt-files/53813/1 "2023-02-28T09:50:24Z")

</div>

Hi, I was trying to plot a histogram from 5 text files having two columns each. The first column is time in order of nanoseconds and second column is number of events. I want an overlay plot of 5 different histograms. I have used the following code.

```auto
import os, sys
import ROOT
from array import *

# Define the file names and their corresponding colors
file_names = ["test1.txt", "test2.txt", "test3.txt", "test4.txt", "test5.txt"]
colors = [1, 2, 3, 4, 6]

# Create an empty list to hold the histograms
hist_list = []

# Loop over the file names and create a histogram for each file
for i, fname in enumerate(file_names):
    f = open(fname)
    lines = f.readlines()
    xValues = []
    yValues = []
    for l in lines:
        if "Time" in l or "LECROY" in l or "Segment" in l or "#" in l or "," not in l:
            continue
        xVal = l.split(" ")[0]
        yVal = l.split(" ")[1].split("\n")[0]
        xValues.append(float(xVal))
        yValues.append(float(yVal))
    binWidth = xValues[1] - xValues[0]
    histo = ROOT.TH1F(fname, fname, len(xValues), xValues[0]-binWidth/2, xValues[-1]+binWidth/2)
    for y in yValues:
        histo.Fill(y)
    histo.SetLineColor(colors[i])
    histo.SetLineWidth(2)
    hist_list.append(histo)

# Draw the histograms on a canvas
can = ROOT.TCanvas("can", "can", 800, 600)
can.cd()
legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)
for i, histo in enumerate(hist_list):
    if i == 0:
        histo.Draw()
    else:
        histo.Draw("SAME")
    legend.AddEntry(histo, file_names[i], "l")
legend.Draw()
can.Update()
can.Print("test.pdf")

sys.exit()

```

This gives the following error-

```auto
 File "hist_txt2.py", line 25, in <module>
    binWidth = xValues[1] - xValues[0]
IndexError: list index out of range

```

Also, the files have different x values from each other. So I want to take the whole range or define it manually. Thanks.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [February 28, 2023, 9:57am UTC](https://root-forum.cern.ch/t/plot-an-overlay-histogram-from-multiple-txt-files/53813/2 "2023-02-28T09:57:20Z")

</div>

As the error says, one index is out of range (most probably `xValues[1]`), so you should make sure what you get from your text files is what you expect…

---

<div class="post-metadata">

**Author:** ![gdash](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@gdash](https://root-forum.cern.ch/u/gdash)\
**Post date:** [February 28, 2023, 10:20am UTC](https://root-forum.cern.ch/t/plot-an-overlay-histogram-from-multiple-txt-files/53813/3 "2023-02-28T10:20:10Z")

</div>

So is it some error emerging from split? Here re the text files for reference.  
[test1.txt](https://root-forum.cern.ch/uploads/short-url/xvUkcQK28QuNLWE8SGZNDHXHlBO.txt) (6.3 KB)  
[test2.txt](https://root-forum.cern.ch/uploads/short-url/qH5c1vfj7yQps5c2QCzy8DVsg28.txt) (64.5 KB)  
[test3.txt](https://root-forum.cern.ch/uploads/short-url/5U9z2ra4TYauCko6D6dkRHZEhWy.txt) (64.2 KB)  
[test4.txt](https://root-forum.cern.ch/uploads/short-url/bzW1kZ1bQvHdrIMQvI8c1giJVtV.txt) (67.3 KB)  
[test5.txt](https://root-forum.cern.ch/uploads/short-url/swN5RzZQPaw872vunoNf77FW7Vd.txt) (63.2 KB)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [February 28, 2023, 10:30am UTC](https://root-forum.cern.ch/t/plot-an-overlay-histogram-from-multiple-txt-files/53813/4 "2023-02-28T10:30:51Z")

</div>

You’re filtering with:

```auto
if "Time" in l or "LECROY" in l or "Segment" in l or "#" in l or "," not in l:
    continue

```

and there is no comma (`,`) in your text files…
