bqplot
is an interactive plotting library. Attributes of plots can be updated in place without recreating the whole figure and marks. Let's look at idiomatic ways of updating plots in bqplot
In [ ]:
import numpy as np
import bqplot.pyplot as plt
In [ ]:
x = np.linspace(-10, 10, 100)
y = np.sin(x)
fig = plt.figure()
line = plt.plot(x=x, y=y)
fig
To update the attributes of the plot(x, y, color etc.) the correct way to do it is to update the attributes of the mark objects in place. Recreating figure or mark objects is not recommended
In [ ]:
# update y attribute of the line object
line.y = np.tan(x)
We can update multiple attributes of the mark object simultaneously by using the hold_sync
method like so. (This makes only one round trip from the python kernel to front end)
In [ ]:
# update both x and y together
with line.hold_sync():
line.x = np.arange(100)
line.y = x ** 3 - x
We can also animate the changes to the x, y and other data attributes by setting the animation_duration
property on the figure object. More examples of animations can found in the Animations notebook
In [ ]:
fig.animation_duration = 1000
line.y = np.cos(x)
Let's look at an example to update a scatter plot
In [ ]:
x, y = np.random.rand(2, 10)
fig = plt.figure(animation_duration=1000)
scat = plt.scatter(x=x, y=y)
fig
In [ ]:
# update the x and y attreibutes in place using hold_sync
with scat.hold_sync():
scat.x, scat.y = np.random.rand(2, 10)