pie plot in matplotlib in Python -


i trying plot pie chart .txt file data-set looks like:

asp: 2.11 glu: 1.11 arg: 0.99 his: 5.11 acid: 11.1 base: 2.11 

now, 1) want plot pie chart first 4 entries, proper labeling.
2) , pie plot using last 2 entries.

i trying following code getting errors. code is:

from pylab import * inp = open('c:/users/rox/desktop/xx.txt','r').read().strip().replace(': ',' ').split('\n') line in map(str.split,inp):     x = line[0]     z = line[1]     fracs = [x]     labels = [z]     pie(fracs,labels=labels,explode=none,autopct='%1.1f%%,shadow=false)     show() 

but code generating error report: not convert string float...

and need use tempfile plot first 4 entries present in .txt file.

if want plot pie chart using last 2 line of data set, done using slicing.

edit: make input more general, multiple plots can read same file:

import matplotlib.pyplot plt  def read_data(f, num_lines=1, split_on=':'):     lines = (f.next() in range(num_lines))     pieces = (line.split(split_on) line in lines)     data = ((a,float(b)) a,b in pieces)     return zip(*data)  open("xx.txt") inf:     amino_names, amino_values = read_data(inf, 4)     ph_names, ph_values = read_data(inf, 2)  fig = plt.figure(figsize=(2,1)) p1 = fig.add_subplot(1,2,1) p1.pie(amino_values, labels=amino_names) p2 = fig.add_subplot(1,2,2) p2.pie(ph_values, labels=ph_names) fig.show() 

results in

enter image description here


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -