import matplotlib.pyplot as plt
plt.figure(figsize=(5,5),dpi=80)
def F(x1,y1,r): #定義遞迴程式
if(r<9) :return
(x2,y2)=(x1+r/3,y1)
(x3,y3)=(x2+r/3,y1)
(x4,y4)=(x1,y1+r/3)
(x5,y5)=(x4+r/3,y4)
(x6,y6)=(x5+r/3,y4)
(x7,y7)=(x1,y4+r/3)
(x8,y8)=(x7+r/3,y7)
(x9,y9)=(x8+r/3,y7)
X=[x5,x6,x9,x8]
Y=[y5,y6,y9,y8]
plt.fill(X,Y,color="white")
F(x1,y1,r/3)
F(x2,y2,r/3)
F(x3,y3,r/3)
F(x4,y4,r/3)
F(x6,y6,r/3)
F(x7,y7,r/3)
F(x8,y8,r/3)
F(x9,y9,r/3)
#以下內容為主程式
(x1,y1,r)=(0,0,81) #正方形左下的坐標與邊長
X=[x1,x1+r,x1+r,x1]
Y=[y1,y1,y1+r,y1+r]
plt.fill(X,Y,color="blue")
F(x1,y1,r)
plt.show()