0

I am simulating an animation using turtle package in python. The effect I want to achieve is like the .gif link I attached here: https://i.stack.imgur.com/prCyQ.gif

(There are 200 concentric shells in my circle. The circle's radius changes over time with my own imported data set, and each shell's radius changes proportionally, each shell has a slight different bluish color but each will also change over time based on my dataset.)

Here is my code generated.

import csv
from turtle import *

def colorConvert(digit:float):
    if digit == 10:
        digit = 'A'
    elif digit == 11:
        digit = 'B'
    elif digit == 12:
        digit = 'C'
    elif digit == 13:
        digit = 'D'
    elif digit == 14:
    digit = 'E'
    elif digit == 15:
        digit = 'F'
    return digit

bgcolor("black")

radius=[]
with open('D:/SURA/Archive/water-loss/output-sorption.csv') as loss_radius:
    reader = csv.reader(loss_radius,delimiter=',')
    for row in reader:
        radius.append(float(row[1]))
conc = []                    # convert to float when accessing elements
with open('D:/SURA/Archive/output-water-concentration.csv') as water:
    reader = csv.reader(water, delimiter=',')
    conc = [list(map(float,row)) for row in reader]

for i in range(301):
    for j in range(200):
        conc[i][j] -= 140

shell_radius=[[0]*200]*301
max=200
radius_max=radius[0]
for i in range(0,301,30):
    for j in range(0,200,20):
        radius_max = radius[i]
        shell_radius[i][j] = radius_max * ((max - j) / max)
        digit5 = int(float(conc[i][j]) // 16)
        digit6 = int(((float(conc[i][j]) / 16) - int(float(conc[i][j]) / 16)) * 16)
        color_set = '#0000'+str(colorConvert(digit5))+str(colorConvert(digit6))
        up()
        goto(0,-shell_radius[i][j]*0.05)
        down()
        pencolor(color_set)    
        fillcolor(color_set)
        begin_fill()
        circle(shell_radius[i][j]*0.05)
        end_fill()

exitonclick()

(the last nested for should run from 0 to 301 and 0 to 200, I shorten them for saving time to show the final visual effect~)

It works, but what I want to optimize is that after one graphing outer for loop finishes executing, the graph can disappear, and starts the next out for loop. Also, is there any way to only show the final graph after each outer for loop execution finishes? So that the demo can be the same as my gif(there is no drawing process displaying).

Thanks in advance!!

1 Answers1

0

You can change turtle's speed while drawing (turtle speed), makes the animation slow at the end of each frame.

Also you can stop for a while using Python's sleep() function, between each frame.

I guess you can instant draw with the fastest speed, and use tracer(False) and hideturtle() to hide it.

nagyl
  • 1,644
  • 1
  • 7
  • 18
  • Can you explain where to use tracer(False)? I tried in the end of each out for loop, but it seems not working. Thanks! – Catherine Kan Jul 06 '20 at 23:39
  • 1
    At the answers, there is a very good explanation. If you did not from turtle import *, then you have to use turtle.tracer(False). https://stackoverflow.com/questions/19619858/turtle-tracer-arguments-example-trouble – nagyl Jul 07 '20 at 13:33