Last active
June 12, 2026 10:47
-
-
Save yellowflash/a40c31e1d7591a5bee9c4dd234a8718b to your computer and use it in GitHub Desktop.
Visualizing Chebyschev Polynomials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from matplotlib.animation import FuncAnimation, PillowWriter | |
| xspace = np.linspace(-1, 1, 500) | |
| thetaspace = np.linspace(np.pi, 0, 500) | |
| centeredthetaspace = np.linspace(-np.pi/2, np.pi/2, 500) | |
| end = [ | |
| np.cos(1 * thetaspace), | |
| np.cos(2 * thetaspace), | |
| np.cos(3 * thetaspace), | |
| np.sqrt(1 - xspace * xspace) | |
| ] | |
| start = [ | |
| np.cos(1 * np.arccos(xspace)), | |
| np.cos(2 * np.arccos(xspace)), | |
| np.cos(3 * np.arccos(xspace)), | |
| np.zeros_like(xspace) | |
| ] | |
| fig, ax = plt.subplots(figsize=(6, 4), layout="constrained") | |
| ax.axis(False) # disable axis | |
| plots = [ | |
| ax.plot(xspace, start[0], lw=2.25, color="red", alpha=0.8)[0], | |
| ax.plot(xspace, start[1], lw=2.25, color="red", alpha=0.5)[0], | |
| ax.plot(xspace, start[2], lw=2.25, color="red", alpha=0.3)[0], | |
| ax.plot(xspace, start[3], lw=2.25, color="#1F0E9E", alpha=0.4)[0] | |
| ] | |
| ax.plot(xspace, np.zeros(500), color="#1F0E9E", lw=2.25) | |
| left = ax.text(-1.05, -0.125, "-1", fontname="Futura", fontsize=8) | |
| right = ax.text(0.98, -0.125, "1", fontname="Futura", fontsize=8) | |
| def frame(frac): | |
| if frac < 0.5: | |
| left.set_text("-1") | |
| right.set_text("1") | |
| else: | |
| left.set_text("π") | |
| right.set_text("0") | |
| def do_plot(i): | |
| s = start[i] | |
| e = end[i] | |
| y = (s + (e - s) * frac) | |
| plots[i].set_data(xspace, y) | |
| return plots[i] | |
| return [do_plot(i) for i in range(len(plots))] | |
| progress = np.concatenate([np.ones(20), np.linspace(1, 0, 100), np.zeros(20), np.linspace(0, 1, 100)]) | |
| FuncAnimation(fig, frame, frames=progress, blit=True).save( | |
| "chebyschev-polynomial.gif", writer=PillowWriter(fps=30), dpi=200, ) |
yellowflash
commented
Jun 11, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment