Python Forum
plot the mean in a bar diagram
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
plot the mean in a bar diagram
#1
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!
Reply
#2
See the content of the file ( backgroundb.txt ). Obviously, the if statement (if len(line) > 1)isn't accurate and is not working.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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.
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to simulate this block diagram with python control library? nugrohoav 1 6,398 May-21-2017, 12:39 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020