绘制线段def drawline(t,x1,y1,x2,y2): t.penup() t.goto(x1,y1) t.pendown() t.goto(x2,y2) #在坐标(x,y)处写文字def drawtext(t,x,y,text): t.penup() t.goto(x,y) t.pendown() t.write(text) ###以上两个函数中t是用于绘制的turtle对象 ####定义drawgraph()实现完整的统计图绘制def drawgraph(t): #绘制x/y轴线 drawline(t,0,0,700,0) drawline(t,0,360,0,0) #x轴:坐标及描述for xin range(count): x=x+1#向右移一位,为了不画在原点上 drawtext(t,x*xscale-4,-20,words[x-1]) drawtext(t,x*60-4,data[x-1]*yscale+10,data[x-1]) drawbar(t) #绘制一个柱体def drawrectangle(t,x,y): x=x*xscale y=y*yscale #以上是显示放大倍数 drawline(t,x-10,0,x-10,y) drawline(t,x-10,y,x+10,y) drawline(t,x+10,y,x+10,0) drawline(t,x+10,0,x-10,0) #绘制多个柱体def drawbar(t): for iin range(count): drawrectangle(t,i+1,data[i]) ####定放对文本的每一行计算词频的函数 processline()def processline(line,wordcounts): #用空格替换标点符号 line=replacepunctuations(line) #从每一行获取每个词 words=line.split() for wordin words: if wordin wordcounts:#对每个出现的词在字典wordcounts中查找 wordcounts[word]+=1#如果该词出现过,为计数器加1else: wordcounts[word]=1