dq.plot.gifit
gifit(plot_function: callable[[T, ...], None]) -> callable[[Sequence[T], ...], Image]
Transform a plot function into a new function that returns an animated GIF.
This function takes a plot function that normally operates on a single input and returns a new function that creates a GIF from a sequence of inputs. The new function accepts two extra keyword arguments:
- gif_duration (float) -- GIF duration in seconds.
- fps (int) -- GIF frames per seconds.
The new function returns an object of type IPython.core.display.Image
,
which automatically displays the GIF in Jupyter notebook environments (when the
Image
object is the last expression in a cell).
Save GIF to a file
The returned GIF can be saved to a file with:
with open('/path/to/file.gif').open('wb') as f:
f.write(gif.data)
Parameters
-
plot_function
–
Plot function which must take as first positional argument the input that will be sequenced over by the new function. It must create a matplotlib
Figure
object and not close it.
Returns
A new function with the same signature as plot_function
which accepts a
sequence of inputs and returns a GIF by applying the original
plot_function
to each element in the sequence.
Examples
>>> def plot_cos(phi):
... x = np.linspace(0, 1.0, 501)
... y = np.cos(2 * np.pi * x + phi)
... plt.figure(constrained_layout=True)
... plt.plot(x, y)
>>> phis = np.linspace(0, 2 * np.pi, 101)
>>> gif = dq.plot.gifit(plot_cos)(phis, fps=25)
>>> gif
<IPython.core.display.Image object>
>>> alphas = jnp.linspace(0.0, 3.0, 51)
>>> states = dq.coherent(24, alphas)
>>> gif = dq.plot.gifit(dq.plot.fock)(states, fps=25)