Error displaying animation

Right now I can run, for example, the following code:

import numpy as np
import math
import hypertools as hyp
%matplotlib inline
arr = np.array( [[math.sin(3*i/100), math.cos (3*i/100), (1/100)**2, (i/100)**3, 1/(1+i/100)] for i in range(0,300)])
hyp.plot(arr)

That works. I’d like to do

hyp.plot(arr, animate=True)

but that returns an error:
Javascript Error: IPython is not defined

Can anyone tell me how to get this to work? I’m using jupyterlab 4.2.3

This is incomplete code or the formatting got messed up because you didn’t use fenced code blocks to share code. (I get SyntaxError: invalid decimal literal concerning the 3i in arr = np.array( [[math.sin(3i/100), math.cos (3i/100), (1/100)**2, (i/100)**3, 1/(1+i/100)] for i in range(0,300)]).) And so I cannot actually test before suggesting things.

I suspect though you are looking to install and use ipympl. See here above the ’ Optional version with a player controller …’ section. You’ll need to transpose it to your code. It should mainly mean changing things to add %matplotlib ipympl at the start of the cell with the animation.

Sorry, here it is again properly (and I edited the original post to fix it)

import numpy as np
import math
import hypertools as hyp
%matplotlib inline
arr = np.array( [[math.sin(3*i/100), math.cos (3*i/100), (1/100)**2, (i/100)**3, 1/(1+i/100)] for i in range(0,300)])
hyp.plot(arr)

I tried using %matplotlib ipympl but I get the same error

It was supposedly fixed in response to this issue report here.
And I know how to get it working for now in fresh sessions via MyBinder:
The additional trick NOW, beyond what is stated here, is you need to specify numpy>=1.10.4 but not 2.0 or above. If you use numpy 2.0 or above, you get AttributeError: np.string_was removed in the NumPy 2.0 release. Usenp.bytes_ instead..
I found v1.26.4 is just fine and here is how you can test using hypertools animation via sessions on MyBinder right now:

  1. Go here and click on launch binder to get a session with ipympl already installed.

  2. When the session comes up, open a new terminal window using the launcher & run in the terminal the following:

    pip install numpy==1.26.4
    
  3. Next in that terminal run the following, based on the advice given here:

    pip install git+https://github.com/ContextLab/hypertools.git
    
  4. Now using the launcher open a new Jupyter Notebook and in the first cell paste this code and run it:

    %matplotlib ipympl
    import math
    import hypertools as hyp
    import numpy as np
    arr = np.array( [[math.sin(3*i/100), math.cos (3*i/100), (1/100)**2, (i/100)**3, 1/(1+i/100)] for i in range(0,300)])
    hyp.plot(arr, animate=True)
    

    You’ll see an animation!

Alternatively, you can go here and use Jeremy Tuloup’s (jtpio) related gist to launch a MyBinder session that doesn’t include ipympl already installed. (As of September 2024 it also has older Python; currently 3.10.14, but it works fine with this process.) Then follow the steps above, until you get to through step #3. You’ll just need to do a hard refresh in the browser window after the step pip install git+https://github.com/ContextLab/hypertools.git, which includes installing ipympl, and before launching a new notebook. That refresh step in Chrome on a Mac is Command + Shift R. I think on Windows it is Shift + F5. Then after the refresh, you can finish the steps above in that session.