斐波那契数列:python实现和可视化( 二 )
4.3 图3代码
#导入模块fromturtle import *#输入自定义数列的个数amount=int(input("请输入斐波那契数列的而个数 , 推荐15:"))#数列产生a1=0a2=1tem_he=0shulie=[]for i in range(1,amount):#终端输出数列的数值print(a1,end=',')tem_he=a1+a2a1=a2a2=tem_he#数列添加数值shulie.append(tem_he)#turtle画图 for i in range(1,amount-1):for n in range(4):forward(shulie[i])left(90)#画圆circle(shulie[i],90)#输出数列数值write(shulie[i],font=("微软雅黑",i*2))mainloop()
4.4 图4代码
# encoding:utf-8#参考文章#;spm=1001.2101.3001.4242import turtlefrom random import randomdef draw_square(r):t.fillcolor(random(), random(), random())t.begin_fill()for _ in range(4):t.fd(r)t.left(90)# 填充关键点,正方形填充一半就结束,然后立即开始圆形填充if _ == 1:t.end_fill()t.fillcolor(random(), random(), random())t.begin_fill()t.circle(r, 90)t.end_fill()returnif __name__ == '__main__':t = turtle.Turtle()t.hideturtle()t.up()t.goto(-130, 50)t.down()t.pensize(3)# 黄金比例scale = 0.618# 初始半径r = 50for _ in range(5):draw_square(r)r /= scaleturtle.mainloop()
4.5 图5
#参看文章:#导出模块import numpy as np#其实就是matplotlib模块import pylab as plt# 产生菲波那切数列def fibo(n):f_0 = 0f_1 = 1fibo_list = np.array([f_0,f_1])for i in np.arange(n-2):fibo_num = f_0 + f_1fibo_list = np.append(fibo_list,fibo_num)f_0, f_1 = f_1, fibo_numreturn fibo_list#找出各个圆心def find_o_xy(f_list):x_n, y_n = 0, 0#起始圆心坐标o_x_array, o_y_array = np.array([x_n]), np.array([y_n])for n in np.arange(1,len(f_list)):#需要注意pyhton中数组计数是从0开始#第一项作为起始点已经给出y_n=y_n+np.mod(n+1,2)*f_list[n]*(-1)**(1+(np.mod(n+1,2)+n+1)/2)x_n=x_n+np.mod(n,2)*f_list[n]*(-1)**(1+(np.mod(n+1,2)+n+1)/2)o_x_array = np.append(o_x_array, x_n)o_y_array = np.append(o_y_array, y_n)return o_x_array, o_y_arraycount = 6#为7是需要调整figsize=(10,10)大小f_list = fibo(count)x0_array,y0_array = find_o_xy(f_list)#------------------------头像4----------------------------f_list_r = fibo(count+2)[2::]start_angle, end_angle = np.pi, 1.5*np.pifig = plt.figure(num=1,facecolor='k',figsize=(10,10))#增加坐标轴对象 , 显示boxax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=True,aspect=1)x_min, x_max, y_min, y_max = 0, 0, 0, 0for n in np.arange(len(f_list_r)):#圆心坐标x0 = x0_array[n]y0 = y0_array[n]#得到对角顶点坐标x2 = x0+f_list_r[n]*(-1)**((np.mod(n+1,2)+n+1)/2)if n == 0:y2 = -1#起始点特殊目前只想到这么整了else:y2 = y0+f_list_r[n]*(-1)**(1+(np.mod(n,2)+n)/2)#画出圆弧t=np.arange(start_angle,end_angle,0.001)circle_x = (f_list_r[n])*(np.sin(t))+x0_array[n]circle_y = (f_list_r[n])*(np.cos(t))+y0_array[n]start_angle += 0.5*np.piend_angle += 0.5*np.pi#画图 , 在坐标轴上画图ax.plot(np.append(x0_array[n],np.append(circle_x,x0_array[n])),np.append(y0_array[n],np.append(circle_y,y0_array[n])),color='k',linewidth=5)ax.fill(np.append(circle_x,x0_array[n]),np.append(circle_y,y0_array[n]),facecolor='gold',alpha = 1)#f5bf03#设置axes内的填充颜色ax.patch.set_facecolor('k')#调节坐标范围x_min,x_max=8.5,-11.5y_min,y_max=10,-10mul_times = 1.5ax.set_xlim(x_min*mul_times, x_max*mul_times)ax.set_ylim(y_min*mul_times, y_max*mul_times)#不显示坐标轴刻度以及刻度线ax.tick_params(axis='both',labelsize=0,length=0)#图片展示plt.show()