Plots is assignment

D​oes anyone know what kind of program or lib can create this gif-plot?

sgd

1 Like

One possible avenue of investigation: matplotlib.animation.FuncAnimation — Matplotlib 3.5.1 documentation

You can find various examples and tutorials on the interweb. Such as: animation example code: simple_anim.py — Matplotlib 2.0.2 documentation

NOTE: the codes in those examples are not running inside Jupyter notebooks, which adds some visualization complexity. You may need to include this line if running within Jupyter

%matplotlib notebook

Let us know what you come up with?

1 Like

Here is a quick and dirty implementation that has some of the automation effect. Doesn’t have dynamic labels, but each feature you add requires going further down the matplotlib rabbit hole. I think if you really want the full monty you need to use FuncAnimation. This will run inside a Jupyter Notebook. My Python is v 3.9.6

%matplotlib notebook

import matplotlib.pyplot as plt 
import numpy as np

x = np.arange(1.,7.1,step=0.1)
y = x**2 * np.sin(x)
y_prime = x*2. * np.cos(x)

x_sub = np.arange(2.5,5.1,step=0.1)
y_sub = x_sub**2 * np.sin(x_sub)

fig, ax = plt.subplots()
line = ax.plot(x,y,color='b')

for i in range(0,26):
    ax.scatter(x_sub[i], y_sub[i], color='r')
    fig.canvas.draw()
    plt.pause(.2)
1 Like
def get_y_prime_str(index,y_prime):
    return f"Derivative: {y_prime[i]}"

for i in range(0,26):
    ax.scatter(x_sub[i], y_sub[i], color='r')
    y_prime_str = get_y_prime_str(i, y_prime)
    ann = plt.annotate(y_prime_str, (x_sub[i],y_sub[i]))
    fig.canvas.draw()
    plt.pause(.2)
    if(i < 25):
        ann.remove()

added another little hack to put a dynamic label in. note that you have to remove all but the last label, though the red dots they are associated with persist.

there are always multiple ways to do things, this is just one not particularly elegant solution. let us know if you write a better one!

1 Like