Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH : Nbagg blit #4290

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/matplotlib/backends/backend_webagg_core.py
Expand Up @@ -138,7 +138,7 @@ def _handle_key(key):


class FigureCanvasWebAggCore(backend_agg.FigureCanvasAgg):
supports_blit = False
supports_blit = True

def __init__(self, *args, **kwargs):
backend_agg.FigureCanvasAgg.__init__(self, *args, **kwargs)
Expand Down Expand Up @@ -180,9 +180,22 @@ def draw(self):
# Swap the frames
self.manager.refresh_all()

def blit(self, bbox=None):
"""
Blit the region in bbox
"""
self._png_is_old = True
self.manager.refresh_all()

def draw_idle(self):
self.send_event("draw")

def copy_from_bbox(self, bbox):
# make sure both renderers exist
self.get_renderer()
renderer = self._last_renderer
return renderer.copy_from_bbox(bbox)

def set_image_mode(self, mode):
"""
Set the image mode for any subsequent images which will be sent
Expand Down Expand Up @@ -241,6 +254,13 @@ def get_diff_image(self):
# Swap the renderer frames
self._renderer, self._last_renderer = (
self._last_renderer, renderer)

# update the cached renders of the Figure and Axes so that
# draw_artist() methods work as expected.
self.figure._cachedRenderer = self._renderer
for ax in self.figure.axes:
ax._cachedRenderer = self._renderer

self._force_full = False
self._png_is_old = False
return self._png_buffer.getvalue()
Expand Down
44 changes: 43 additions & 1 deletion lib/matplotlib/backends/web_backend/nbagg_uat.ipynb
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:a1ac68aba163c75eab3d1fc91aa4d9a8ca66b09159619563827a19967d96814b"
"signature": "sha256:b18e2b0bb3d6c7d68852e1691d2a961bb7c0dd6b254113a763473bbe83dd4f0e"
},
"nbformat": 3,
"nbformat_minor": 0,
Expand Down Expand Up @@ -439,6 +439,48 @@
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### UAT 17 - Blitting\n",
"\n",
"Clicking on the figure should plot a green horizontal line moving up the axes.\n",
"\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"prev_artist = None\n",
"cur_y = 0.1\n",
"\n",
"def onclick_handle(event):\n",
" \"\"\"Should draw elevating green line on each mouse click\"\"\"\n",
" global ax, bg, prev_artist, cur_y\n",
" ax.figure.canvas.restore_region(bg)\n",
" if prev_artist:\n",
" prev_artist.remove()\n",
" cur_y += 0.1\n",
" prev_artist = ax.plot([0, 1], [cur_y, cur_y], 'g')[0]\n",
" ax.draw_artist(prev_artist)\n",
" ax.figure.canvas.blit(ax.bbox)\n",
" print(\"Drew line\")\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot([0, 1], [0, 1], 'r')\n",
"plt.show()\n",
"ax.figure.canvas.draw()\n",
"bg = ax.figure.canvas.copy_from_bbox(ax.bbox)\n",
"ax.figure.canvas.mpl_connect('button_press_event', onclick_handle)"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
Expand Down