Skip to content

Commit a23c9a3

Browse files
committed
Updated nbagg backend to mimic savefig defaults, *not* interactive ones.
1 parent 3e4620a commit a23c9a3

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Interactive figures in the IPython notebook"""
22
from base64 import b64encode
3+
from contextlib import contextmanager
34
import json
45
import io
56
import os
@@ -10,11 +11,14 @@
1011
from IPython.display import display, Javascript, HTML
1112
from IPython.kernel.comm import Comm
1213

14+
from matplotlib import rcParams
1315
from matplotlib.figure import Figure
16+
from matplotlib.backends import backend_agg
1417
from matplotlib.backends.backend_webagg_core import (FigureManagerWebAgg,
1518
FigureCanvasWebAggCore,
1619
NavigationToolbar2WebAgg)
17-
from matplotlib.backend_bases import ShowBase, NavigationToolbar2, TimerBase
20+
from matplotlib.backend_bases import (ShowBase, NavigationToolbar2,
21+
TimerBase, FigureCanvasBase)
1822

1923

2024
class Show(ShowBase):
@@ -36,6 +40,25 @@ def __call__(self, block=None):
3640
show = Show()
3741

3842

43+
@contextmanager
44+
def savefig_background(figure):
45+
"""Implement the savefig background color logic."""
46+
orig_facecolor = figure.patch.get_facecolor()
47+
orig_edgecolor = figure.patch.get_edgecolor()
48+
49+
if rcParams['savefig.transparent']:
50+
figure.patch.set_facecolor('none')
51+
figure.patch.set_edgecolor('none')
52+
else:
53+
figure.patch.set_facecolor(rcParams['savefig.facecolor'])
54+
figure.patch.set_edgecolor(rcParams['savefig.edgecolor'])
55+
56+
yield
57+
58+
figure.patch.set_facecolor(orig_facecolor)
59+
figure.patch.set_edgecolor(orig_edgecolor)
60+
61+
3962
def draw_if_interactive():
4063
from matplotlib import is_interactive
4164
import matplotlib._pylab_helpers as pylab_helpers
@@ -179,6 +202,26 @@ class FigureCanvasNbAgg(FigureCanvasWebAggCore):
179202
def new_timer(self, *args, **kwargs):
180203
return TimerTornado(*args, **kwargs)
181204

205+
def start_event_loop(self, timeout):
206+
FigureCanvasBase.start_event_loop_default(self, timeout)
207+
208+
def stop_event_loop(self):
209+
FigureCanvasBase.stop_event_loop_default(self)
210+
211+
def draw(self):
212+
renderer = self.get_renderer()
213+
214+
self._png_is_old = True
215+
216+
backend_agg.RendererAgg.lock.acquire()
217+
try:
218+
with savefig_background(self.figure):
219+
self.figure.draw(renderer)
220+
finally:
221+
backend_agg.RendererAgg.lock.release()
222+
# Swap the frames
223+
self.manager.refresh_all()
224+
182225

183226
def new_figure_manager(num, *args, **kwargs):
184227
"""

lib/matplotlib/backends/web_backend/nbagg_uat.ipynb

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:9a73a15660e6912c6bcfaf7c9e8247503738ce5d590a34e08789c2e16de93080"
4+
"signature": "sha256:bdd1e05f7e181c8ffb90423b07f7badc827c8f51d2b6ca8c06ce6715789c70e7"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -92,7 +92,7 @@
9292
"cell_type": "code",
9393
"collapsed": false,
9494
"input": [
95-
"print matplotlib.backends.backend_nbagg.connection_info()"
95+
"print(matplotlib.backends.backend_nbagg.connection_info())"
9696
],
9797
"language": "python",
9898
"metadata": {},
@@ -354,9 +354,38 @@
354354
"cell_type": "markdown",
355355
"metadata": {},
356356
"source": [
357-
"### UAT ?? - Keyboard shortcuts in IPython after close of figure (Current known bug)\n",
358-
"### UAT ?? - Race condition to show means that \"run all\" ends up collapsing some figures into one (Current known bug)"
357+
"### UAT 14 - Keyboard shortcuts in IPython after close of figure\n",
358+
"\n",
359+
"After closing the previous figure (with the close button above the figure) the IPython keyboard shortcuts should still function.\n",
360+
"\n",
361+
"### UAT 15 - Savefig colors\n",
362+
"\n",
363+
"The nbagg backend should honour savefig colors - not the GUI values. The plot below should produce a figure with a yellow background - there should be no green or red in the plot."
359364
]
365+
},
366+
{
367+
"cell_type": "code",
368+
"collapsed": false,
369+
"input": [
370+
"import matplotlib\n",
371+
"matplotlib.rcParams.update({'figure.facecolor': 'red',\n",
372+
" 'patch.facecolor': 'green',\n",
373+
" 'savefig.facecolor': 'yellow'})\n",
374+
"plt.figure()\n",
375+
"plt.plot([3, 2, 1])\n",
376+
"plt.show()"
377+
],
378+
"language": "python",
379+
"metadata": {},
380+
"outputs": []
381+
},
382+
{
383+
"cell_type": "code",
384+
"collapsed": false,
385+
"input": [],
386+
"language": "python",
387+
"metadata": {},
388+
"outputs": []
360389
}
361390
],
362391
"metadata": {}

0 commit comments

Comments
 (0)