如何将程序生成的数据传输到新的Turtle程序中使用?

时间:2019-07-29 01:58:12

标签: python turtle-graphics chaos

下面看到的是我的迭代程序的代码。我希望能够使用乌龟图形来获取每个参数(k),并根据其对应的k值绘制方程式输出。如果我没有记错的话,这应该创建一个feigenbaum图?我的问题是,如何让海龟为每个k值绘制这些点,然后将它们连接到相邻k值的点,依此类推?

def iteration(xstore):
    global x0
    x0=xstore
    print (x0)

x0=float(input("x0:"))
n=float(input("max parameter value:"))
divison=float(input("divisons between parameters:"))

xv=x0
x1=0
k=0

while k<(n+divison):
    print("K VALUE:"+str(k))
    for i in range (0,20):
        x1=x0+x0*k*(1-x0)
        iteration(x1)

print ("________________________")

x0=xv

k=k+divison

2 个答案:

答案 0 :(得分:0)

这是使用tkinter生成的feigenbaum图。它来自“打开的图书项目” visualizing chaos

程序源为here;我将其转换为python 3并将其发布在下面。您有很多东西可以学习阅读和理解此代码。

enter image description here

#
#   chaos-3.py
#
#  Build Feigenbaum Logistic map. Input start and end K
#
#  python chaos-3.py 3.4 3.9  
#

canWidth=500
canHeight=500

def setupWindow () :
    global win, canvas
    from tkinter import Tk, Canvas, Frame
    win = Tk()
    canvas = Canvas(win, height=canHeight, width=canWidth)
    f = Frame (win)    
    canvas.pack()
    f.pack()

def startApp () :
    global win, canvas
    import sys
#     k1  = float(sys.argv[1])   # starting value of K
#     k2  = float(sys.argv[2])   # ending   value of K
    x = .2                     # is somewhat arbitrary
    vrng = range(200)          # We'll do 200 horz steps
    for t in range(canWidth) :
        win.update()
        k = k1 + (k2-k1)*t/canWidth
#         print("K = %.04f" % k)
        for i in vrng :
            p = x*canHeight
            canvas.create_line(t,p,t,p+1)  # just makes a pixel dot
            x = x * (1-x) * k              # next x value
            if x <=0 or x >= 1.0 :
#                 print("overflow at k", k)
                return

def main () :
    setupWindow()       # Create Canvas with Frame
    startApp()          # Start up the display  
    win.mainloop()      # Just wait for user to close graph


k1 = 2.9
k2 = 3.8
main()

答案 1 :(得分:0)

  

如何让乌龟为每个k值绘制这些点

这是我使用Python乌龟得出的一个简单,粗略,缓慢的示例:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 800, 400

Kmin = 2.5
Kmax = 3.8
x = 0.6

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.setworldcoordinates(Kmin, 0.0, Kmax, 1.0)
screen.tracer(False)

turtle = Turtle()
turtle.hideturtle()
turtle.penup()

k = Kmin

while k < Kmax:
    for _ in range(HEIGHT//4):
        x *= (1.0 - x) * k

        turtle.goto(k, x)
        turtle.dot(2)

        x *= 1 + 1/(HEIGHT//4)

    k *= 1 + 1/WIDTH

screen.tracer(True)
screen.exitonclick()

enter image description here

我希望它能给您一些有关使用乌龟绘制函数的想法。 (当然,最后结合使用matplotlib和numpy通常效果更好。)