0

In matplotlib, I can make a gif with “FuncAnimation” and export it with the method “to_jshtml()” that returns an HTML. The output adds a “scrubber” widget to the animation, which is great. Here is a reproducible example:

import xarray as xr
from matplotlib import pyplot as plt, animation
from IPython.display import HTML

ds = xr.tutorial.open_dataset('air_temperature')
tas=ds.air
tas = tas - 273.15

# Get a handle on the figure and the axes
fig, ax = plt.subplots(figsize=(12,6))

# Plot the initial frame. 
cax = tas[0,:,:].plot(
    add_colorbar=True,
    cmap='coolwarm',
    vmin=-40, vmax=40,
    cbar_kwargs={
        'extend':'neither'
    }
)

# Next we need to create a function that updates the values for the colormesh, as well as the title.
def animate(frame):
    cax.set_array(tas[frame,:,:].values.flatten())
    ax.set_title("Time = " + str(tas.coords['time'].values[frame])[:13])

# Finally, we use the animation module to create the animation.
ani = animation.FuncAnimation(
    fig,             # figure
    animate,         # name of the function above
    frames=40,       # Could also be iterable or list
    interval=200     # ms between frames
)

# Then
HTML(ani.to_jshtml())

I would like to do the same with bokeh, i.e. I want to make a gif with the “scrubber” widget and export it to HTML, but taking advantage of the bokeh customisation possibilities.

There is a way to do this?

I expected a Bokeh solution for this, Bokeh is more powerful tool.

2
  • Could you please check out if hvplot animations are what you are looking for. The example uses the same date you use and the solution is based on bokeh. Commented Jun 3, 2024 at 11:45
  • that would be the ideal, but the output of that hvplot function is a panel object, I need a bokeh object to embed the figure in a webpage. It seems that I can achieve my goal with javascript callbacks, I will be reading about that. Anyway, thank you. Commented Jun 9, 2024 at 21:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.