Ovisek Ovisek - 3 years ago 108
Python Question

why do i'm getting overlapping pie plot in python

I am walking along a directory using

walk
function > searching for a .cnt file > counting interested value > plotting 'em in a pie chart. But the problem is when my prog processes for the first folder and plots, it acts fine.But after that whatever folder it reads and plots it get overlapped. Can't understand if bug in my code. I used like:

for root,dirs,files in os.walk(path):
aspCount = 0
gluCount = 0
aspCountCol1 = 0
aspCountCol2 = 0
gluCountCol1 = 0
gluCountCol2 = 0

listOfFile = glob.iglob(os.path.join(root,'*.cnt'))
for filename in listOfFile:
inp = open(filename,'r').read().strip().split('\n')
for line in map(str.split,inp):

k = line[-1]
m = line[0]
if k == 'ASP':
aspCountCol1 += 1
elif m == 'ASP':
aspCountCol2 += 1
if k == 'GLU':
gluCountCol1 += 1
elif m == 'GLU'
gluCountCol1 +=1
# here lies the problem for me !!!!
aspCount = aspCountCol1 + aspCountCol1
gluCount = gluCountCol1 + gluCountCol1
#now plotting......
from pylab import *
figure(1, (figsize=(8,8))
labels = 'asp','glu'
fracs = [asp_count,glu_count]
pie(fracs,explode=None,labels=labels,autopct='%1.1f%%',shadow=False)
c = 'fig.png'
savefig(os.path.join(root,c))


now, the problem is: with this code if i process a directory with various sub-folders containing .cnt file, it goes error free. But the graph generated by the first folder, it be fine but when prog goes to process the next folder it sucessfully process the data but the graph generated is overlapped with the previous one.

the file i was processing was:

LYS ARG
ASP GLU
GLU SAP
JAS ASP
SAK GLU

Answer Source

You need to clear your pylab plots between plots with clf().

Also, no reason to from pylab import * multiple times.

(I prefer to use the matplotlib api when plotting in a loop like that)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download