Question: My interactive plots in jupyter notebook using python updates way too slow. I'm hoping someone can show me perhaps a more optimal plotting library for
My interactive plots in jupyter notebook using python updates way too slow. I'm hoping someone can show me perhaps a more optimal plotting library for interactive plots than matplotlib, or show me how to speed up the update speed. I've written a sample code to show what I mean. Say I want to plot the function y = A*sin(B*x), but I want A and B to be sliders. I can do this with the following code:
# Interactive Plot Example
%matplotlib inline import matplotlib.pyplot as plt import numpy as np from ipywidgets import interactive, FloatSlider, IntSlider
xvals = np.linspace(0,2*np.pi,100) def myFunction(A,B,x): return A*np.sin(B*x)
def plot(A,B): yval = myFunction(A,B,xvals) plt.plot(xvals,yval) plt.title("Plotting $y = \sin (x)$") plt.xlabel("$x$") plt.ylabel("$y$") plt.ylim(-5,5) plt.xlim(0,2*np.pi) plt.ylim plt.grid() plt.show() interactive_plot = interactive(plot,A = IntSlider(min = 1, max = 5, step = 1,value = 1)\ ,B = FloatSlider(min = 0,max = 2.5,step = 0.1,value = 1)) output = interactive_plot.children[-1] output.layout.height = '350px' interactive_plot
This works, but as I said, the frame rate is way too slow. Not to mention, this is just an example. The code I'm actually working with has many more sliders, a higher number of points, and several more plt. function calls. Can anyone make suggestions on how to speed up the plot updates? Perhaps if theres a way to decrease the pixel count or something that I don't know about? I've also heard mpld3 can be usefull, but I've never used it. Thanks!
Note: I'm not sure why chegg won't let me indent the code, but obviously the first function should have the return indented. Also in the plot function, everything up to and including plt.show() should be indented.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
