What we can do is convert the animation into an HTML5 video and embed it in Jupyter Notebook. Of course, we could take a screen capture, but that’s not efficient when you want to share your Jupyter Notebook online. However, the plot is animating only when the code is running. In the previous example, we have created a nice animation with linear regression. Please check out Notebook for source code 3. Matplotlib Linear Regression Animation in Jupyter Notebook (Image by Author) In general, we need FPS greater than 16 for smooth animation (Human eyes can only receive 10–12 frames ).Īnim = FuncAnimation(fig, animate, frames=len(x), interval=20) plt.show() If the number is too large you wait a really long time, if the number is too small, it would be faster than your eyes could see. Finally, the interval=20 argument sets the delay (in milliseconds) between frames.The third argument frames=len(x), it defines the number of frames for “one round of animation” and we have set it to the number of training data.The second argument animate is the function we created to call at each frame to update the plot.The first argument fig is the reference to the figure we created.def animate(frame_num): # Adding data x_data.append(x) y_data.append(y) # Convert data to numpy array x_train = np.array(x_data).reshape(-1, 1) y_train = np.array(y_data).reshape(-1, 1) # Fit values to a linear regression reg.fit(x_train, y_train) # update data for scatter plot t_data((x_data, y_data)) # Predict value and update data for line plot t_data( (list(range(250)), reg.predict(np.array().reshape(-1, 1))))įinally, we create our animation object by calling FuncAnimation with 4 arguments Notice that reg.fit() is called to fit values as more data is added. What we want to do here is to change data for our scatter plot and linear regression plot according to the frame number. The function takes one argument frame_num - the current frame number. We then create a function animate() that’s going to be called by the FuncAnimation(). Miles_per_Gallon') line, = ax.plot(,, 'r', label='Linear Regression') ax.legend() reg = LinearRegression() x_data = y_data = fig, ax = plt.subplots() ax.set_xlim(30, 250) ax.set_ylim(5, 50) scatter, = ax.plot(,, 'go', label='Horsepower vs. We also call LinearRegression() to create a Linear Regression model reg. ax.plot() is called twice to create a scatter plot and a line plot. We set the x range to (30, 250) and y range to (5, 50) to avoid them to be constantly changing. We call subplots() without any arguments to create a Figure fig and a single Axes ax. Both x_data and y_data are initialized to. Next, we need to create the initial state of the animation figure. import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from sklearn.linear_model import LinearRegression Especially FuncAnimation class for creating animation and LinearRegression for creating a Linear Regression model. In order to create an interactive plot in Jupyter Notebook, you first need to enable interactive plot as follows: # Enable interactive plot %matplotlib notebookĪfter that, we import the required libraries. More tutorials are available from Github Repo. Please check out the Notebook for source code.
0 Comments
Leave a Reply. |