实验6 turtle绘图与python库应用编程体验
实验任务一
task1-1:
源代码:
1 from turtle import * 2 3 def move(x,y): 4 penup() 5 goto(x,y) 6 pendown() 7 8 def draw(n,size = 100): 9 for i in range(n): 10 fd(size) 11 left(360/n) 12 13 def main(): 14 pensize(2) 15 pencolor('red') 16 17 move(-200,0) 18 draw(3) 19 20 move(0,0) 21 draw(4) 22 23 move(200,0) 24 draw(5) 25 26 hideturtle() 27 done() 28 29 main()
运行结果:
task1-2:
源代码:
1 from turtle import * 2 3 def moveto(x,y): 4 penup() 5 goto(x,y) 6 pendown() 7 8 def main(): 9 pensize(2) 10 pencolor('blue') 11 12 moveto(-150,0) 13 circle(50) 14 15 moveto(0,0) 16 circle(50,steps=4) 17 18 moveto(150,0) 19 circle(50,steps=5) 20 21 moveto(300,0) 22 circle(50,steps=6) 23 24 hideturtle() 25 done() 26 27 main()
运行结果:
实验任务二
task2-1:
源代码:
1 from turtle import * 2 3 def moveto(x,y): 4 penup() 5 goto(x,y) 6 pendown() 7 8 def main(): 9 setup(800,600) 10 11 radius = 20 12 offset = 20 13 14 for i in range(9): 15 moveto(0,-radius) 16 circle(radius) 17 radius += offset 18 19 hideturtle() 20 done() 21 22 main()
运行结果:
task2-2:
源代码:
1 from turtle import * 2 from random import random 3 4 def moveto(x,y): 5 penup() 6 goto(x,y) 7 pendown() 8 9 def gen_color(): 10 return tuple(random() for i in range(3)) 11 12 def main(): 13 setup(800,600) 14 15 radius = 180 16 offset = 20 17 18 for i in range(8): 19 moveto(0,-radius) 20 color(gen_color()) 21 22 begin_fill() 23 circle(radius) 24 end_fill() 25 26 radius -= offset 27 28 hideturtle() 29 done() 30 31 main()
运行结果:
实验任务三
task3-1:
源代码:
1 from turtle import * 2 3 def square(size = 50, rgb = 'pink'): 4 pencolor(rgb) 5 for i in range(4): 6 fd(size) 7 left(90) 8 9 def main(): 10 setup(800,600) 11 speed(0) 12 13 14 n = 10 15 for i in range(n): 16 square(80) 17 left(360/n) 18 19 hideturtle() 20 done() 21 22 main()
运行结果:
task3-2:
源代码:
1 from turtle import * 2 3 setup(800,600) 4 pencolor('pink') 5 6 n = 10 7 for i in range(10): 8 for j in range(2): 9 circle(80,90) 10 left(90) 11 12 right(360/n) 13 14 hideturtle() 15 done()
运行结果:
实验任务四
task4-1:
源代码:
1 from turtle import * 2 3 setup(800,600) 4 bgcolor('black') 5 pencolor('white') 6 speed(0) 7 8 angle = 0 9 size = 2 10 11 n = 5 12 count = 50 13 for i in range(count): 14 fd(size) 15 angle += 360/n 16 seth(angle) 17 size += 5 18 19 hideturtle() 20 done()
运行结果:
实验任务五
task5-1:
源代码:
1 from turtle import * 2 import math 3 4 setup(800,600) 5 def moveto(x,y): 6 penup() 7 goto(x,y) 8 pendown() 9 10 n = 4 11 moveto(-100,-100) 12 color('black') 13 begin_fill() 14 for i in range(n): 15 fd(200) 16 left(360/n) 17 end_fill() 18 19 moveto(0,-100) 20 color('red') 21 begin_fill() 22 for i in range(n): 23 left(360/n/2) 24 fd(100*math.sqrt(2)) 25 left(360/n/2) 26 end_fill() 27 28 hideturtle() 29 done()
运行结果:
task5-2:
源代码:
1 from turtle import * 2 3 setup(800,600) 4 5 pencolor('blue') 6 pensize(2) 7 8 n=5 9 radius = 40 10 offset = 20 11 for i in range(5): 12 if i == 0: 13 goto(radius,0) 14 left(90) 15 circle(radius,extent=90) 16 goto(0,0) 17 else: 18 goto(radius,0) 19 right(90) 20 circle(radius,extent=90) 21 goto(0,0) 22 23 radius += offset 24 25 hideturtle() 26 done()
运行结果:
实验任务六
task6-1:
源代码:
1 from turtle import * 2 from random import random 3 4 setup(800,600) 5 6 def fillcolor(): 7 return tuple(random() for i in range(3)) 8 9 def moveto(x,y): 10 penup() 11 goto(x,y) 12 pendown() 13 14 def rect(c,k): 15 for i in range(4): 16 if i in [0,2]: 17 fd(k) 18 left(360/4) 19 else: 20 fd(c) 21 left(360/4) 22 23 def main(): 24 for j in range(20): 25 c,k = 100,30 26 x_lst = [i for i in range(-300,301,30)] 27 y = 0 28 29 color(fillcolor()) 30 begin_fill() 31 moveto(x_lst[j],y) 32 rect(c,k) 33 end_fill() 34 35 hideturtle() 36 done() 37 38 main()
运行结果:
task6-2:
源代码:
1 from turtle import * 2 3 bgcolor('black') 4 setup(800,600) 5 6 def moveto(x,y): 7 penup() 8 goto(x,y) 9 pendown() 10 11 radius = 150 12 color('yellow') 13 begin_fill() 14 moveto(radius/2**0.5,radius/2**0.5) 15 left(135) 16 circle(radius,extent=360-90) 17 end_fill() 18 19 radius1 = 15 20 color('black') 21 begin_fill() 22 moveto(0,radius/2) 23 circle(radius1) 24 end_fill() 25 26 radius2 = 5 27 color('white') 28 begin_fill() 29 moveto(-radius1,radius/2) 30 circle(radius2) 31 end_fill() 32 33 hideturtle() 34 done()
运行结果:
实验任务七
task7:
源代码:
1 from matplotlib import pyplot as plt 2 3 def func(x): 4 return 4*x*(1-x) 5 6 def gen_lst(x,n): 7 ans = [] 8 for i in range(n): 9 t = func(x) 10 ans.append(t) 11 x = t 12 return ans 13 14 def main(): 15 n = 30 16 17 lst1 = gen_lst(0.2,n) 18 lst2 = gen_lst(0.2000001,n) 19 20 x = list(range(1,31)) 21 plt.plot(x,lst1,'ro-',x,lst2,'bs-') 22 plt.xticks(list(range(1,31,4))) 23 plt.legend(['x = 0.2','x = 0.2000001']) 24 plt.show() 25 26 main()
运行结果:
实验任务八
task8:
源代码:
1 import jieba 2 from wordcloud import WordCloud 3 from matplotlib import pyplot as plt 4 5 text = '''requests是一个常用的HTTP请求库,可以方便地向网站发送HTTP请求,并获取响应结 6 果。 7 Scrapy是一个开源和协作框架,用于从网站中提取数据,是最流行的爬虫框架。 8 SciPy是一个开源的Python算法库和数学工具包,它基于Numpy,用于数学、科学、工程学等领 9 域。''' 10 11 word = jieba.lcut(text) 12 13 t = ' '.join(word) 14 t_wc = WordCloud(font_path = 'msyh.ttc').generate(t) 15 16 t_wc.to_file('wordcloud.png') 17 18 plt.imshow(t_wc) 19 plt.axis('off') 20 plt.show()
运行结果: