Skip to content

Commit 2eff4d1

Browse files
committed
Apply gtk3 background.
Currently, gtk3 widget backgrounds are not being applied (which only matters if the figure patch is partially transparent). The lines added by this PR are equivalent to the call to `painter.eraseRect(self.rect())` in the qt5agg backend (compare http://doc.qt.io/qt-5/qpainter.html#eraseRect and https://developer.gnome.org/gtk3/stable/GtkStyleContext.html#gtk-render-background). As an example, run import os; os.environ["MPLBACKEND"] = "gtk3cairo" from pylab import * from gi.repository import Gtk rcParams["figure.facecolor"] = (0, 0, 0, 0) css = Gtk.CssProvider() css.load_from_data(b"* {background-color: red;}") gcf().canvas.get_style_context().add_provider( css, Gtk.STYLE_PROVIDER_PRIORITY_USER) gca() show() Without this patch, the red background is not drawn.
1 parent 4753f6c commit 2eff4d1

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/matplotlib/backends/backend_gtk3agg.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from . import backend_agg, backend_gtk3
1010
from .backend_cairo import cairo, HAS_CAIRO_CFFI
11-
from .backend_gtk3 import _BackendGTK3
11+
from .backend_gtk3 import Gtk, _BackendGTK3
1212
from matplotlib import transforms
1313

1414
if six.PY3 and not HAS_CAIRO_CFFI:
@@ -37,6 +37,10 @@ def on_draw_event(self, widget, ctx):
3737

3838
if not len(self._bbox_queue):
3939
self._render_figure(w, h)
40+
Gtk.render_background(
41+
self.get_style_context(), ctx,
42+
allocation.x, allocation.y,
43+
allocation.width, allocation.height)
4044
bbox_queue = [transforms.Bbox([[0, 0], [w, h]])]
4145
else:
4246
bbox_queue = self._bbox_queue

lib/matplotlib/backends/backend_gtk3cairo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from . import backend_cairo, backend_gtk3
77
from .backend_cairo import cairo, HAS_CAIRO_CFFI
8-
from .backend_gtk3 import _BackendGTK3
8+
from .backend_gtk3 import Gtk, _BackendGTK3
99
from matplotlib.backend_bases import cursors
1010

1111

@@ -39,6 +39,9 @@ def on_draw_event(self, widget, ctx):
3939
# toolbar.set_cursor(cursors.WAIT)
4040
self._renderer.set_context(ctx)
4141
allocation = self.get_allocation()
42+
Gtk.render_background(
43+
self.get_style_context(), ctx,
44+
allocation.x, allocation.y, allocation.width, allocation.height)
4245
self._render_figure(allocation.width, allocation.height)
4346
# if toolbar:
4447
# toolbar.set_cursor(toolbar._lastCursor)

0 commit comments

Comments
 (0)