我在逛Quora的时候发现这么一个问题:
What is the most beautiful program you ever coded in Python?
有网友分享了自己用turtle或者pylab画出来的酷炫图形。
前两个是用turtle画的动态图(简书可以传动图吗?),第三个是用pylab画的静态图,原来用pylab也能画出这么多彩的图形。
from turtle import*
from time import sleep
title("fanatic")
bgcolor("white")
speed(0)
ht()
pu()
goto(-400,0)
pd()
tracer(False)
L=["green","yellow"]
for i in range(5,101,2):
for j in range(i):
for z in range(40):
pencolor(L[z%2])
fd(20)
rt(180-180/i)
tracer(True)
sleep(0.03)
tracer(False)
clear()
'''
效果图
'''
from turtle import *
from random import randint
speed(0)
bgcolor('white')
x = 1
while x < 400:
r = randint(0,255) #让颜色随机变化
g = randint(0,255)
b = randint(0,255)
colormode(255)
pencolor(r,g,b) # 根据r,g,b的值改变笔触颜色
fd(50 + x)
rt(90.911)
x = x+1
exitonclick()
'''
效果图
'''
from pylab import *
from numpy import NaN
def m(a):
z = 0
for n in range(1, 100):
z = z**2 + a
if abs(z) > 2:
return n
return NaN
X = arange(-2, .5, .002)
Y = arange(-1, 1, .002)
Z = zeros((len(Y), len(X)))
for iy, y in enumerate(Y):
print (iy, "of", len(Y))
for ix, x in enumerate(X):
Z[iy,ix] = m(x + 1j * y)
imshow(Z, cmap = plt.cm.prism, interpolation = 'none', extent = (X.min(), X.max(), Y.min(), Y.max()))
xlabel("Re(c)")
ylabel("Im(c)")
savefig("mandelbrot_python.png")
show()
'''
效果图