Nanocartography: Planning for success in analytical electron microscopy

# Widget for displaying movie animations.

%matplotlib widget

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from mpl_toolkits.axes_grid1 import make_axes_locatable
from IPython.display import HTML
import numpy as np
import imageio as iio

vid = iio.get_reader("data/GVectorRotation.mp4")
num_frames = vid.count_frames()

dpi = 70
plot_every_frame = 6
width = 400



plt.ioff()
fig, ax = plt.subplots(figsize=(width/dpi, width/dpi), dpi=dpi)
ax.axis('off')

array = vid.get_data(0)
array = array[:,:,0] # grayscale
array = 255 - array # reverse
nx, ny = array.shape

int_mean = np.mean(array)
int_std = np.sqrt(np.mean((array - int_mean)**2))

init_min = int_mean -1 * int_std
init_max = int_mean + 2 * int_std

im=ax.imshow(array,cmap='Greys',vmin=init_min,vmax=init_max)
divider = make_axes_locatable(ax)
ax_cb = divider.append_axes("right", size="5%", pad="2.5%")
fig.add_axes(ax_cb)
fig.colorbar(im, cax=ax_cb)
fig.tight_layout()

ax.plot((20,70),(ny-20,ny-20),lw=5,color='k')
ax.text((70+20)/2,ny-20-10,"2 nm",color='k',ha='center')

def display_frame(index):
    array = vid.get_data(index*plot_every_frame)[:,:,0] # grayscale image
    array = 255 - array # reverse
    im.set_data(array)
    fig.canvas.draw_idle()

    return None
    
animation = FuncAnimation(
    fig,
    display_frame,
    frames=num_frames//plot_every_frame,
    repeat=False,
    blit=False,
    interval=50,
)

HTML(animation.to_jshtml())