diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 99db4957b277..0bb1db9f97ba 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1709,10 +1709,13 @@ def supports_blit(cls): return (hasattr(cls, "copy_from_bbox") and hasattr(cls, "restore_region")) - def __init__(self, figure): + def __init__(self, figure=None): + from matplotlib.figure import Figure self._fix_ipython_backend2gui() self._is_idle_drawing = True self._is_saving = False + if figure is None: + figure = Figure() figure.set_canvas(self) self.figure = figure self.manager = None diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index 992e2a7b7eb1..8a541adbe29e 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -162,7 +162,7 @@ class FigureCanvasTk(FigureCanvasBase): @_api.delete_parameter( "3.4", "resize_callback", alternative="get_tk_widget().bind('', ..., True)") - def __init__(self, figure, master=None, resize_callback=None): + def __init__(self, figure=None, master=None, resize_callback=None): super().__init__(figure) self._idle = True self._idle_callback = None diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index 4c63505352c2..68e43ca2b32b 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -102,7 +102,7 @@ class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase): | Gdk.EventMask.POINTER_MOTION_HINT_MASK | Gdk.EventMask.SCROLL_MASK) - def __init__(self, figure): + def __init__(self, figure=None): FigureCanvasBase.__init__(self, figure) GObject.GObject.__init__(self) diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index 4a63e79d220b..afb2bcb483e8 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -206,13 +206,13 @@ class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase): } @_allow_super_init - def __init__(self, figure): + def __init__(self, figure=None): _create_qApp() super().__init__(figure=figure) # We don't want to scale up the figure DPI more than once. # Note, we don't handle a signal for changing DPI yet. - figure._original_dpi = figure.dpi + self.figure._original_dpi = self.figure.dpi self._update_figure_dpi() # In cases with mixed resolution displays, we need to be careful if the # dpi_ratio changes - in this case we need to resize the canvas diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index 90eafca5334d..897c28c38f04 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -16,7 +16,7 @@ class FigureCanvasQTAgg(FigureCanvasAgg, FigureCanvasQT): - def __init__(self, figure): + def __init__(self, figure=None): # Must pass 'figure' as kwarg to Qt base class. super().__init__(figure=figure) diff --git a/lib/matplotlib/backends/backend_qt5cairo.py b/lib/matplotlib/backends/backend_qt5cairo.py index a375d1f4d1e2..4b6d7305e7c1 100644 --- a/lib/matplotlib/backends/backend_qt5cairo.py +++ b/lib/matplotlib/backends/backend_qt5cairo.py @@ -6,7 +6,7 @@ class FigureCanvasQTCairo(FigureCanvasQT, FigureCanvasCairo): - def __init__(self, figure): + def __init__(self, figure=None): super().__init__(figure=figure) self._renderer = RendererCairo(self.figure.dpi) self._renderer.set_width_height(-1, -1) # Invalid values. diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index a5f335cbbc32..421601039fc9 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -494,7 +494,7 @@ class _FigureCanvasWxBase(FigureCanvasBase, wx.Panel): wx.WXK_NUMPAD_DELETE: 'delete', } - def __init__(self, parent, id, figure): + def __init__(self, parent, id, figure=None): """ Initialize a FigureWx instance. @@ -504,7 +504,7 @@ def __init__(self, parent, id, figure): """ FigureCanvasBase.__init__(self, figure) - w, h = map(math.ceil, figure.bbox.size) + w, h = map(math.ceil, self.figure.bbox.size) # Set preferred window size hint - helps the sizer, if one is connected wx.Panel.__init__(self, parent, id, size=wx.Size(w, h)) # Create the drawing bitmap diff --git a/lib/matplotlib/tests/test_backend_bases.py b/lib/matplotlib/tests/test_backend_bases.py index 25183f7a06e2..7ca260c187ef 100644 --- a/lib/matplotlib/tests/test_backend_bases.py +++ b/lib/matplotlib/tests/test_backend_bases.py @@ -5,9 +5,11 @@ NavigationToolbar2, RendererBase) from matplotlib.backend_tools import (ToolZoom, ToolPan, RubberbandBase, ToolViewsPositions, _views_positions) +from matplotlib.figure import Figure import matplotlib.pyplot as plt import matplotlib.transforms as transforms import matplotlib.path as path + import numpy as np import pytest @@ -53,6 +55,10 @@ def check(master_transform, paths, all_transforms, check(id, paths, tforms_matrices, offsets, facecolors[0:1], edgecolors) +def test_canvas_ctor(): + assert isinstance(FigureCanvasBase().figure, Figure) + + def test_get_default_filename(tmpdir): plt.rcParams['savefig.directory'] = str(tmpdir) fig = plt.figure()