Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Last active May 29, 2025 17:59
Show Gist options
  • Save nicoguaro/735ef9c606a2a94eb415a9f061ab5ad0 to your computer and use it in GitHub Desktop.
Save nicoguaro/735ef9c606a2a94eb415a9f061ab5ad0 to your computer and use it in GitHub Desktop.
Tori spectra
# -*- coding: utf-8 -*-
"""
Based on
https://www.instagram.com/reel/DJpQwikz9Yh/
[-i 0 t1 0 0 t2]
[ 0 0 x x 0 x]
A = [ i 0 0 x 0 i]
[ 0 0 0 -i -i i]
[-i x 0 0 -i x]
[-i x 0 0 0 -i]
@author: Nicolás Guarín-Zapata
@date: May 2025
"""
import os
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import eigvals
from PIL import Image
def save_gif_PIL(outfile, files, fps=5, loop=0):
"""Helper function for saving GIFs
Parameters
----------
outfile : string
Path to the output file.
files : list
List of paths with the PNG files.
fps : int (optional)
Frames per second.
loop : int
The number of times the GIF should loop.
0 means that it will loop forever.
"""
imgs = [Image.open(file) for file in files]
imgs[0].save(fp=outfile, format='GIF', append_images=imgs[1:],
save_all=True, duration=int(1000/fps), loop=loop)
#%% Image generation
repo = "https://raw.githubusercontent.com/nicoguaro/matplotlib_styles/master"
style = repo + "/styles/neon.mplstyle"
plt.style.use(style)
col1 = "#04D9D9"
npts = 101
niter = 20
files = []
plt.figure(figsize=(4, 4))
for cont, x in enumerate(np.linspace(0, 2, niter)):
for θ1 in np.linspace(0, 2*np.pi, npts):
for θ2 in np.linspace(0, 2*np.pi, npts):
t1 = np.exp(1j*θ1)
t2 = np.exp(1j*θ2)
A = np.array([
[-1j, 0, t1, 0, 0, t2],
[0, 0, x, x, 0, x],
[1j, 0, 0, x, 0, 1j],
[0, 0, 0, -1j, -1j, 1j],
[-1j, x, 0, 0, -1j, x],
[-1j, x, 0, 0, 0, -1j]], dtype=complex)
evals = eigvals(A)
plt.plot(evals.real, evals.imag, ".", mfc=col1, mec="None",
markersize=2, alpha=0.5)
plt.axis("off")
file = f"torus_spectra{str(cont).zfill(2)}.png"
plt.savefig(file, dpi=600)
files.append(file)
plt.cla()
#%% Animation
save_gif_PIL("torus_spectra_anim.gif", files, fps=5, loop=0)
[os.remove(file) for file in files]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment