1

Note: only the last code chunk brings an error. The earlier chunks are to give context to the animation that I want. This is all in Jupyter for Windows.

I have a matplotlib pyplot with two line segments, one is "wrong" and the other is "right." I want to animate the graph to start with both lines where the blue "wrong" line is,and have the red one pivot and move to be in the right place. The "wrong" line goes from (x,y) = (-1.25,9.1) to (0.75,8.0). The "right" line goes from (-1.25,9.7) to (0.75,7.5) Here is the code for the static comparison:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
boring_fig = plt.figure()
blue = plt.plot([-1.25,.75], [9.1,8], color = 'b', label = 'wrong')
red = plt.plot([-1.25,.75], [9.7,7.5], color = 'r', label = 'right')
plt.show()

Now I want to start them both where the blue line is, and then have the red line incrementally move to the correct position. I made these two arrays for y coordinates to incrementally change between lines. Then I graph everything but the red line, in hopes of adding it as an animation after this.

y_left = np.array([9.1, 9.16, 9.22, 9.28, 9.34, 9.4, 9.46, 9.52, 9.58, 9.64, 9.7])
y_right = np.array([8.0, 7.95, 7.9, 7.85, 7.8, 7.75, 7.7, 7.65, 7.6, 7.55, 7.5])

fig = plt.figure()
blue = plt.plot([-1.25,.75], [9.1,8], color = 'b', label = 'wrong')
plt.show()

And then I try to animate the red line segment shifting along those incremented y values. I get the error somewhere in here:

def animate_1(i):
    return plt.plot([-1.25,.75], [y_left[i],y_right[i]], color = 'r'),
   
anim = FuncAnimation(fig = fig, func = animate_1, interval = 100, frames = 10)
plt.show(anim)

And I get this message: "UserWarning: Animation was deleted without rendering anything. This is most likely unintended. To prevent deletion, assign the Animation to a variable that exists for as long as you need the Animation." I spent hours trying to figure this out, but I am too much of a noob. Please help.

Matt Birch
  • 21
  • 1
  • 4

2 Answers2

1

It turns out I had to install ffmpeg for windows:

https://www.wikihow.com/Install-FFmpeg-on-Windows

Now the graph shows up but I can't see it animating. Oh well. That is an entirely different question.

here is the end graph I made (with a couple of extra details)

Matt Birch
  • 21
  • 1
  • 4
  • Hopefully, you got your animation working even though I didn't see you post another question. In case it helps someone later, I have [a GitHub repository here](https://github.com/fomightez/animated_matplotlib-binder) that you can launch as an active Jupyter session with a notebook that has some useful code & pointers for animations using Jupyter notebooks. There's some on the use of `FuncAnimation()` towards the bottom. Sometimes having examples that work or a different environment to test in can help you sort things out as a lot of moving parts can be in play. Plus I had a note that I had ... – Wayne Sep 14 '22 at 16:09
  • to remove the HTML5 video-making portion since I didn't have ffmpeg installed in that demo environment. – Wayne Sep 14 '22 at 16:14
0

Add this line to the beginning of your code in order to see the animation work:

%matplotlib notebook

In Jupyter Notebook (at least on Windows) specifying the GUI toolkit will allow you to see the animation updating, not just a static graph. You can read more about toolkits and user interfaces here.

If there is a message that says:

Warning: Cannot change to a different GUI toolkit: ...

then restart the Jupyter kernel and run the cell again to update the GUI. For troubleshooting the GUI, see this answer.

In regards to the original question, if you set blit = False in FuncAnimation then the animation should display. Otherwise, you can add a return blue, statement to the animate function. See my code below and feel free to ask questions!

Complete Code

%matplotlib notebook

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation

def move_line(num_frames):
    x_vals = [-1.25,.75]
    y_left_array = np.linspace(9.1, 9.7, num=num_frames, endpoint=True)
    y_right_array = np.linspace(8.0, 7.5, num=num_frames, endpoint=True)
    
    fig = plt.figure()
    red = plt.plot(x_vals, [9.7,7.5], color = 'r', label = 'right')
    blue, = plt.plot([], [], color = 'b', label = 'wrong')

    def animate(I):
        y_vals = [y_left_array[i],y_right_array[I]]
        blue.set_data(x_vals,y_vals)
        return blue,

    anim = FuncAnimation(
        fig,
        animate,
        frames = num_frames,
        interval = 1000/30,
        repeat = False,
        blit = True
    )

    plt.show()

    return anim

animate_lines = move_line(100)
trent
  • 359
  • 1
  • 9