<jupyter_start><jupyter_text>UAT for NbAgg backend.The first line simply reloads matplotlib, uses the nbagg backend and then reloads the backend, just to ensure we have the latest modification to the backend code. Note: The underlying JavaScript will not be updated by this process, so a refresh of the browser after clearing the output and saving is necessary to clear everything fully.<jupyter_code>import matplotlib
reload(matplotlib)

matplotlib.use('nbagg')

import matplotlib.backends.backend_nbagg
reload(matplotlib.backends.backend_nbagg)<jupyter_output><empty_output><jupyter_text>UAT 1 - Simple figure creation using pyplotShould produce a figure window which is interactive with the pan and zoom buttons. (Do not press the close button, but any others may be used).<jupyter_code>import matplotlib.backends.backend_webagg_core
reload(matplotlib.backends.backend_webagg_core)

import matplotlib.pyplot as plt
plt.interactive(False)

fig1 = plt.figure()
plt.plot(range(10))

plt.show()<jupyter_output><empty_output><jupyter_text>UAT 2 - Creation of another figure, without the need to do plt.figure.As above, a new figure should be created.<jupyter_code>plt.plot([3, 2, 1])
plt.show()<jupyter_output><empty_output><jupyter_text>UAT 3 - Connection infoThe printout should show that there are two figures which have active CommSockets, and no figures pending show.<jupyter_code>print(matplotlib.backends.backend_nbagg.connection_info())<jupyter_output><empty_output><jupyter_text>UAT 4 - Closing figuresClosing a specific figure instance should turn the figure into a plain image - the UI should have been removed. In this case, scroll back to the first figure and assert this is the case.<jupyter_code>plt.close(fig1)<jupyter_output><empty_output><jupyter_text>UAT 5 - No show without plt.show in non-interactive modeSimply doing a plt.plot should not show a new figure, nor indeed update an existing one (easily verified in UAT 6).The output should simply be a list of Line2D instances.<jupyter_code>plt.plot(range(10))<jupyter_output><empty_output><jupyter_text>UAT 6 - Connection informationWe just created a new figure, but didn't show it. Connection info should no longer have "Figure 1" (as we closed it in UAT 4) and should have figure 2 and 3, with Figure 3 without any connections. There should be 1 figure pending.<jupyter_code>print(matplotlib.backends.backend_nbagg.connection_info())<jupyter_output><empty_output><jupyter_text>UAT 7 - Show of previously created figureWe should be able to show a figure we've previously created. The following should produce two figure windows.<jupyter_code>plt.show()
plt.figure()
plt.plot(range(5))
plt.show()<jupyter_output><empty_output><jupyter_text>UAT 8 - Interactive modeIn interactive mode, creating a line should result in a figure being shown.<jupyter_code>plt.interactive(True)
plt.figure()
plt.plot([3, 2, 1])<jupyter_output><empty_output><jupyter_text>Subsequent lines should be added to the existing figure, rather than creating a new one.<jupyter_code>plt.plot(range(3))<jupyter_output><empty_output><jupyter_text>Calling connection_info in interactive mode should not show any pending figures.<jupyter_code>print(matplotlib.backends.backend_nbagg.connection_info())<jupyter_output><empty_output><jupyter_text>Disable interactive mode again.<jupyter_code>plt.interactive(False)<jupyter_output><empty_output><jupyter_text>UAT 9 - Multiple showsUnlike most of the other matplotlib backends, we may want to see a figure multiple times (with or without synchronisation between the views, though the former is not yet implemented). Assert that plt.gcf().canvas.manager.reshow() results in another figure window which is synchronised upon pan & zoom.<jupyter_code>plt.gcf().canvas.manager.reshow()<jupyter_output><empty_output><jupyter_text>UAT 10 - Saving notebookSaving the notebook (with CTRL+S or File->Save) should result in the saved notebook having static versions of the figues embedded within. The image should be the last update from user interaction and interactive plotting. (check by converting with ``ipython nbconvert ``)  UAT 11 - Creation of a new figure on second showCreate a figure, show it, then create a new axes and show it. The result should be a new figure.**BUG: Sometimes this doesn't work - not sure why (@pelson).**<jupyter_code>fig = plt.figure()
plt.axes()
plt.show()

plt.plot([1, 2, 3])
plt.show()<jupyter_output><empty_output><jupyter_text>UAT 12 - OO interfaceShould produce a new figure and plot it.<jupyter_code>from matplotlib.backends.backend_nbagg import new_figure_manager,show

manager = new_figure_manager(1000)
fig = manager.canvas.figure
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3])
fig.show()<jupyter_output><empty_output><jupyter_text>UAT 13 - AnimationThe following should generate an animated line:<jupyter_code>import matplotlib.animation as animation
import numpy as np

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)        # x-array
line, = ax.plot(x, np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x+i/10.0))  # update the data
    return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
                              interval=32., blit=True)
plt.show()<jupyter_output><empty_output><jupyter_text>UAT 14 - Keyboard shortcuts in IPython after close of figureAfter closing the previous figure (with the close button above the figure) the IPython keyboard shortcuts should still function. UAT 15 - Figure face coloursThe nbagg honours all colours apart from that of the figure.patch. The two plots below should produce a figure with a red background. There should be no yellow figure.<jupyter_code>import matplotlib
matplotlib.rcParams.update({'figure.facecolor': 'red',
                            'savefig.facecolor': 'yellow'})
plt.figure()
plt.plot([3, 2, 1])

plt.show()<jupyter_output><empty_output><jupyter_text>UAT 16 - EventsPressing any keyboard key or mouse button (or scrolling) should cycle the line line while the figure has focus.  The figure should have focus by default when it is created and re-gain it by clicking on the canvas.  Clicking anywhere outside of the figure should release focus, but moving the mouse out of the figure should not release focus.<jupyter_code>import itertools
fig, ax = plt.subplots()
x = np.linspace(0,10,10000)
y = np.sin(x)
ln, = ax.plot(x,y)
evt = []
colors = iter(itertools.cycle(['r', 'g', 'b', 'k', 'c']))
def on_event(event):
    if event.name.startswith('key'):
        fig.suptitle('%s: %s' % (event.name, event.key))
    elif event.name == 'scroll_event':
        fig.suptitle('%s: %s' % (event.name, event.step))
    else:
        fig.suptitle('%s: %s' % (event.name, event.button))
    evt.append(event)
    ln.set_color(next(colors))
    fig.canvas.draw()
    fig.canvas.draw_idle()

fig.canvas.mpl_connect('button_press_event', on_event)
fig.canvas.mpl_connect('button_release_event', on_event)
fig.canvas.mpl_connect('scroll_event', on_event)
fig.canvas.mpl_connect('key_press_event', on_event)
fig.canvas.mpl_connect('key_release_event', on_event)

plt.show()<jupyter_output><empty_output><jupyter_text>UAT 17 - TimersSingle-shot timers follow a completely different code path in the nbagg backend than regular timers (such as those used in the animation example above.)  The next set of tests ensures that both "regular" and "single-shot" timers work properly.The following should show a simple clock that updates twice a second:<jupyter_code>import time

fig, ax = plt.subplots()
text = ax.text(0.5, 0.5, '', ha='center')

def update(text):
    text.set(text=time.ctime())
    text.axes.figure.canvas.draw()
    
timer = fig.canvas.new_timer(500, [(update, [text], {})])
timer.start()
plt.show()<jupyter_output><empty_output><jupyter_text>However, the following should only update once and then stop:<jupyter_code>fig, ax = plt.subplots()
text = ax.text(0.5, 0.5, '', ha='center') 
timer = fig.canvas.new_timer(500, [(update, [text], {})])

timer.single_shot = True
timer.start()

plt.show()<jupyter_output><empty_output><jupyter_text>And the next two examples should never show any visible text at all:<jupyter_code>fig, ax = plt.subplots()
text = ax.text(0.5, 0.5, '', ha='center')
timer = fig.canvas.new_timer(500, [(update, [text], {})])

timer.start()
timer.stop()

plt.show()
fig, ax = plt.subplots()
text = ax.text(0.5, 0.5, '', ha='center')
timer = fig.canvas.new_timer(500, [(update, [text], {})])

timer.single_shot = True
timer.start()
timer.stop()

plt.show()<jupyter_output><empty_output><jupyter_text>UAT17 - stopping figure when removed from DOMWhen the div that contains from the figure is removed from the DOM the figure should shut down it's comm, and if the python-side figure has no more active comms, it should destroy the figure. Repeatedly running the cell below should always have the same figure number<jupyter_code>fig, ax = plt.subplots()
ax.plot(range(5))
plt.show()<jupyter_output><empty_output><jupyter_text>Running the cell below will re-show the figure. After this, re-running the cell above should result in a new figure number.<jupyter_code>fig.canvas.manager.reshow()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10464616046461604, 'fraction_numerical': 0.012475801247580125, 'mean_word_length': 5.5439831104855735, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 94, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '35799', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2944, 'n_words': 941, 'path': 'extern_libs/Python27/lib/python2.7/site-packages/matplotlib/backends/web_backend/nbagg_uat.ipynb', 'repo_name': 'onceawaken/MKL-DNN_Eigen_Boost_OpenMPI_GoogleTests_Examples', 'size': '9298'}