# pip install celluloid
#%matplotlib qt
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import hsv_to_rgb
from celluloid import Camera

fig = plt.figure()
camera = Camera(fig)
for a in np.linspace(0, 2 * np.pi, 30, endpoint=False):
    x = np.linspace(-3, 3, 800)
    X, Y = np.meshgrid(x, x)
    x = X + 1j * Y
    y = (x ** 2 - 2.5) * (x - 2.5 * 1j) * (x + 2.5 * 1j) \
        * (x - 2 - 1j) ** 2 / ((x - np.exp(1j * a)) ** 2
        * (x - np.exp(1j * 2 * a)) ** 2)
    H = np.angle(y) / (2 * np.pi) + .5
    r = np.log2(1. + np.abs(y))
    S = (1. + np.abs(np.sin(2. * np.pi * r))) / 2.
    V = (1. + np.abs(np.cos(2. * np.pi * r))) / 2.
    rgb = hsv_to_rgb(np.dstack((H, S, V)))
    plt.imshow(rgb)
    camera.snap()

animation = camera.animate()
animation.save('ex355.gif', writer='PillowWriter', fps=24)