Python中用turtle库画树形

本文介绍了一种使用Python的Turtle模块绘制复杂树形图案的方法。通过递归调用tree函数,可以创建出多分支的树状结构。文章详细展示了如何通过调整参数来改变树的形状,包括树干长度、分支角度和颜色。通过修改代码,读者可以尝试绘制出不同的图形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复制代码到Python中运行:

#coding=utf-8
import turtle
import time
turtle.tracer(False)   #1、这行代码如果注释后再运行,是每一画都显示;2、如不注释就运行,直接出结果
def tree(branchLen,t):
    if branchLen > 5:
        t.forward(branchLen)
        
        t.right(20)                     		 #右转20度
        tree(branchLen-15,t)            #画枝丫,代码越多越密
        tree(branchLen-30,t)            #画枝丫,代码越多越密
        tree(branchLen-10,t)            #画枝丫,代码越多越密
        tree(branchLen-20,t)            #画枝丫,代码越多越密
        tree(branchLen-25,t)            #画枝丫,代码越多越密
        tree(branchLen-35,t)            #画枝丫,代码越多越密
        tree(branchLen-40,t)            #画枝丫,代码越多越密
        tree(branchLen-45,t)            #画枝丫,代码越多越密
        
        t.left(40)
        tree(branchLen-15,t)
        tree(branchLen-30,t)
        tree(branchLen-10,t)
        tree(branchLen-20,t)
        tree(branchLen-25,t)
        tree(branchLen-35,t)
        tree(branchLen-40,t)
        tree(branchLen-45,t)
        t.right(20)
        t.backward(branchLen)

def main():
    t = turtle.Turtle()
    myWin = turtle.Screen()
    time.sleep(2)
    t.left(0)
    t.up()                  #海龟尾巴向上,移动不划线
    t.backward(15)
    t.down()                #海龟尾巴向下
    t.color("green")         #设置画笔颜色,绿色
    tree(75,t)              #枝丫的幅度
    tree(60,t)              #枝丫的幅度
    tree(30,t)              #枝丫的幅度
    tree(10,t)              #枝丫的幅度
    
    t.left(90)
    t.up()#海龟尾巴向上,移动不划线
    t.backward(15)
    t.down()#海龟尾巴向下
    t.color("gold")#设置画笔颜色,金色
    tree(75,t)
    tree(60,t)
    tree(30,t)
    tree(10,t)
    
    t.left(180)
    t.up()#海龟尾巴向上,移动不划线
    t.backward(15)
    t.down()#海龟尾巴向下
    t.color("black")#设置画笔颜色,黑色
    tree(75,t)
    tree(60,t)
    tree(30,t)
    tree(10,t)
     
    t.left(270)
    t.up()#海龟尾巴向上,移动不划线
    t.backward(15)
    t.down()#海龟尾巴向下
    t.color("red")#设置画笔颜色,红色
    tree(75,t)
    tree(60,t)
    tree(30,t)
    tree(10,t)
  
    myWin.exitonclick()
main()

所得图形,中间代码改改,能得到其他图形

Python 有很多可以状图,其中比较流行的有 `matplotlib`、`graphviz` 和 `networkx`。 以下是使用 `matplotlib` 和 `networkx` 两个状图的示例代码: 使用 `matplotlib`: ```python import matplotlib.pyplot as plt # 定义节点和边 nodes = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('C', 'G')] # 图 pos = {node: (idx, -idx) for idx, node in enumerate(nodes)} plt.figure(figsize=(6, 6)) nx.draw_networkx_nodes(nodes, pos, node_size=2000, node_color='lightblue') nx.draw_networkx_labels(nodes, pos, font_size=20, font_family='sans-serif') nx.draw_networkx_edges(edges, pos, width=2, alpha=0.5, edge_color='gray') plt.axis('off') plt.show() ``` 使用 `networkx`: ```python import networkx as nx import matplotlib.pyplot as plt # 定义节点和边 nodes = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('C', 'G')] # 图 plt.figure(figsize=(6, 6)) G = nx.DiGraph() G.add_nodes_from(nodes) G.add_edges_from(edges) pos = nx.spring_layout(G) nx.draw_networkx_nodes(G, pos, node_size=2000, node_color='lightblue') nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif') nx.draw_networkx_edges(G, pos, width=2, alpha=0.5, edge_color='gray') plt.axis('off') plt.show() ``` 这两个示例代码都会生成一个状图,其中节点用字母表示,边用线段表示。您可以根据需要自行修改节点和边的定义,以及调整参数来美化图形
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值