0

i have a .txt file containing data like:

He: 22.1
Ar: 21.1
K:  1.22
U:  0.09
P:  22.0

now what I wanted to do is to plot a pie chart using line 2 to line 4. I have managed to plot a similar one using first 4 lines using this code:

f=open(filename,'r')
line = (f.next() for i in range(4))
pieces = (lin.split(':') for lin in line)
data = (a, float(b)) for a, b in pieces)
labels,values = zip(*data)
plt.pie(values,labels=labels)

using this code I could managed to draw a similar pie of first 4 lines. But in case of selective plotting using line2 and line4 how do i use slicing here to take desired lines out.

1
  • Yes, you should use slicing. Since you're aware of the concept, have you tried it at all, and can you post the code? Or do you just not understand how it works? (Also posting your actual plotting code might be useful) Commented Jun 10, 2012 at 17:56

1 Answer 1

0

That's rather simple really, if I understand what you're asking. Change your file reading line to

line = f.readlines ()

which will put all of the data into the list. Then change your lasts line to

plt.pie (values [start:stop], labels=labels [start:stop])

where you've defined start and stop beforehand, of course. So for lines 2 to 4 you would set start = 2 and stop = 5. Is that what you're after?

Sign up to request clarification or add additional context in comments.

8 Comments

no! I wanted to plot using line 2 and line 4...how to jump and collect values for plotting...
For regular jumps (eg line 2, 4, 6, 8...), you can just use [start:stop:step] in place of [start:stop] above (so for lines 2 and 4 that would be [1::2]). For arbitrary selections, you might want to have a look at using numpy, which has a take() method for its arrays, and generally can do a few more tricks than standard Python indexing.
I actually have to draw 2 graph...first one using line2 and line4(done using step) and the second one using all lines except line2 and line4...i'm getting second one a bit difficult for me...
For the table you've listed, that would just be [0::2]. However if you had a list that had more than 5 elements in it, and you still wanted 'all except lines 2 & 4', then that won't work. If you need non-structured indices, you'll need to use something like numpy, where you can do things like values [[0,2,4,5,6,9,8]] to pick out whatever you need.
yeah! I have got a tutorial of numpy.take but how do i feed those data to the graph variables...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.