Skip to content

Commit 1b96117

Browse files
committed
Updated nbagg backend to default to a transparent figure patch.
1 parent 3e4620a commit 1b96117

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

lib/matplotlib/backends/backend_nbagg.py

+26-1
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):
@@ -179,6 +183,25 @@ class FigureCanvasNbAgg(FigureCanvasWebAggCore):
179183
def new_timer(self, *args, **kwargs):
180184
return TimerTornado(*args, **kwargs)
181185

186+
def start_event_loop(self, timeout):
187+
FigureCanvasBase.start_event_loop_default(self, timeout)
188+
189+
def stop_event_loop(self):
190+
FigureCanvasBase.stop_event_loop_default(self)
191+
192+
def draw(self):
193+
renderer = self.get_renderer()
194+
195+
self._png_is_old = True
196+
197+
backend_agg.RendererAgg.lock.acquire()
198+
try:
199+
self.figure.draw(renderer)
200+
finally:
201+
backend_agg.RendererAgg.lock.release()
202+
# Swap the frames
203+
self.manager.refresh_all()
204+
182205

183206
def new_figure_manager(num, *args, **kwargs):
184207
"""
@@ -194,6 +217,8 @@ def new_figure_manager_given_figure(num, figure):
194217
Create a new figure manager instance for the given figure.
195218
"""
196219
canvas = FigureCanvasNbAgg(figure)
220+
if rcParams['nbagg.transparent']:
221+
figure.patch.set_alpha(0)
197222
manager = FigureManagerNbAgg(canvas, num)
198223
return manager
199224

lib/matplotlib/backends/web_backend/nbagg_uat.ipynb

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:9a73a15660e6912c6bcfaf7c9e8247503738ce5d590a34e08789c2e16de93080"
4+
"signature": "sha256:34c3a5a9d82d5a3c433f0c03f0717d43e1d14eaf78f88a64f094d104fa636ddc"
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,42 @@
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 - Figure face colours\n",
362+
"\n",
363+
"The nbagg honours all colours appart from that of the figure.patch. The two plots below should produce a figure with a transparent background and a red background respectively (check the transparency by closing the figure, and dragging the resulting image over other content). There should be no yellow figure."
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+
" 'savefig.facecolor': 'yellow'})\n",
373+
"plt.figure()\n",
374+
"plt.plot([3, 2, 1])\n",
375+
"\n",
376+
"with matplotlib.rc_context({'nbagg.transparent': False}):\n",
377+
" plt.figure()\n",
378+
"\n",
379+
"plt.plot([3, 2, 1])\n",
380+
"plt.show()"
381+
],
382+
"language": "python",
383+
"metadata": {},
384+
"outputs": []
385+
},
386+
{
387+
"cell_type": "code",
388+
"collapsed": false,
389+
"input": [],
390+
"language": "python",
391+
"metadata": {},
392+
"outputs": []
360393
}
361394
],
362395
"metadata": {}

lib/matplotlib/rcsetup.py

+1
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ def __call__(self, s):
485485
'webagg.port': [8988, validate_int],
486486
'webagg.open_in_browser': [True, validate_bool],
487487
'webagg.port_retries': [50, validate_int],
488+
'nbagg.transparent': [True, validate_bool],
488489
'toolbar': ['toolbar2', validate_toolbar],
489490
'datapath': [None, validate_path_exists], # handled by
490491
# _get_data_path_cached

matplotlibrc.template

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ backend : %(backend)s
5757
# When True, open the webbrowser to the plot that is shown
5858
# webagg.open_in_browser : True
5959

60+
# When True, the figures rendered in the nbagg backend are created with
61+
# a transparent background.
62+
# nbagg.transparent : True
63+
6064
# if you are running pyplot inside a GUI and your backend choice
6165
# conflicts, we will automatically try to find a compatible one for
6266
# you if backend_fallback is True

0 commit comments

Comments
 (0)