[python] Clock that changes color (animation)

Introduction

Creates a second hand (clock) that rotates every second and makes one revolution in 60 seconds. Just drawing the second hand is boring, so the color of the clock should gradually change from blue to red in the range of 0 to 60 [s]. Finally, save it as a gif and you're done.

Use python's library for creating animations.

Completed animation

20191204second_hand.gif

code

second_hand.py


%matplotlib nbagg

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.patches as pat

fig = plt.figure()
ax = plt.subplot()

def clock(i):
    circle = [ax.add_patch(pat.Wedge(center=(0, 0), r=1, color=[0+i/60,0,1-i/60], theta1 = 95-i*(360/60), theta2 = 85-i*(360/60)))]
    #center: center xy coordinates,r: wedge radius,color: Color specification in RGB(Each color 0~1),theta: Specify the wedge angle
    #Note that we list circles. This is so that it can be added to imgs later.
    return circle

#Add a clock for each second to the list imgs.
imgs=[]
for i in range(60):
    imgs.append(clock(i))
    
ani = animation.ArtistAnimation(fig, imgs, interval=1000, repeat=True)
plt.axis("scaled")
plt.show()

ani.save("second_hand.gif", writer="imagemagick")#Save as gif

environment

macOS Catalina jupyter-notebook

Recommended Posts

[python] Clock that changes color (animation)
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[ev3dev × Python] Color sensor
blender, python, spiral staircase, color
[Python] Adjusting the color bar
Blender 2.9, Python, hexadecimal color specification
Note that it supports Python 3
Movement that changes direction in the coordinate system I tried Python 3