Python Forum
plot the mean in a bar diagram - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: plot the mean in a bar diagram (/thread-10507.html)



plot the mean in a bar diagram - sussii - May-23-2018

Hi, I need some help with writing a code.
I have a text file where it contains number of decays per second. I want to make a bar diagram of the distribution of number of decays per 5 second intervals and calculate and enter mean and plot in the same graph the poisson distribution, normalized to the number of events in the chart that has this mean.

I dont really understand how to do and appreciate all the help I can get.

I started this way:

import numpy as np
from spicy.stats import poisson
from matplotlib import pyplot as plt

#I think that we want to take the readings from the text file (it called backgroundb)

with open('backgroundb.txt') as f:
lines = [line.strip().split(',') for line in f if len(line) > 1]
labels, y = zip(*lines)
ind = np.arange(len(labels))
plt.figure()
plt.bar(ind, y, align='center')
plt.xticks(ind, labels)
plt.show()
The text file contains:
0 5
1 10
2 1
3 2
4 8
5 1
6 4
7 6
8 1
9 3
(contains more values but I wanted to show how the text file looks like)
I have the text file in my desktop and I do not know if it depends on it and I do not know how to continue.

I get error like:
Traceback (most recent call last):
File "C:\Users\sibel\Desktop\labb.py", line 8, in <module>
labels, y=zip(*lines)
ValueError: not enough values to unpack (expected 2, got 1)

And i dont know what I need to change.

All help is appreciated!


RE: plot the mean in a bar diagram - wavic - May-23-2018

See the content of the file ( backgroundb.txt ). Obviously, the if statement (if len(line) > 1)isn't accurate and is not working.


RE: plot the mean in a bar diagram - sussii - May-23-2018

(May-23-2018, 11:11 PM)wavic Wrote: See the content of the file ( backgroundb.txt ). Obviously, the if statement (if len(line) > 1)isn't accurate and is not working.

But what can I write instead of if len(line) > 1? The textfile contains 352 seconds and decay for each second.


RE: plot the mean in a bar diagram - wavic - May-23-2018

Oh! In your post, you are showing the file content. Well, why do you split by comma where the values are separated by space? There is no comma, so there is no splitting of the line and you get the whole line - one value.

And why zip()? You get already a list of two values which you can unpack.
>>> line = "4 8"
>>> line.split()
['4', '8']
The last one is stupig. Don't pay attention on it. :D