python 【折线图】

 1 import turtle
 2 yValues = [10.0,7.4,6.4,5.3,10.4,3.7,2.6]
 3 def main():
 4     t = turtle.Turtle()
 5     t.hideturtle()
 6     drawLine(t,0,0,300,0)  #画x轴
 7     drawLine(t,0,0,0,175)  #画y轴
 8     for i in range(6):
 9         drawLineWithDots(t,40+(40*i),15*yValues[i],40+(40*(i+1)),15*yValues[i+1],"blue")
10     
11     drawTickMarks(t)
12     displayText(t)
13 def drawLine(t,x1,y1,x2,y2,colorP="black"):  #画两点的连线
14     t.up()
15     t.goto(x1,y1)
16     t.down()
17     t.pencolor(colorP)
18     t.goto(x2,y2)
19 def drawLineWithDots(t,x1,y1,x2,y2,colorP="black"): #画两点的点
20     t.pencolor(colorP)
21     t.up()
22     t.goto(x1,y1)
23     t.dot(5)
24     t.down()
25     t.goto(x2,y2)
26     t.dot(5)
27 def drawTickMarks(t):                         #画x,y轴的节点
28     for i in range(1,8):
29         drawLine(t,40*i,0,40*i,10)              #x轴节点
30     drawLine(t,0,15*max(yValues),10,15*max(yValues))    #y轴最大
31     drawLine(t,0,15*min(yValues),10,15*min(yValues))    #y轴最小值
32 def displayText(t):
33     t.pencolor("blue")
34     t.up()
35     t.goto(-3,(15*max(yValues))-10)
36     t.write(max(yValues),align="right")        #y轴标点数值
37     t.goto(-3,(15*min(yValues))-10)
38     t.write(min(yValues),align="right")           #y轴标点数值
39 
40     x=40
41     for i in range(2000,2013,2):
42         t.goto(x,-20)
43         t.write(str(i),align="center")
44         x+=40
45         t.goto(0,-40)
46         t.write("percentage of college freshman who smoke")
47 main()
48         

 

posted @ 2018-03-27 19:27  Justice-V  阅读(204)  评论(0)    收藏  举报