Skip to content

Commit ce951b1

Browse files
committed
Make figure parameter optional when constructing canvases.
Auto-instantiating a Figure seems reasonable enough, and helps with GUI builders. (The auto-instantiated figure must be a plain Figure(), not a a pyplot-registered figure, both because most likely one should not use pyplot when building GUIs, and also more prosaically because that would run into a circular dependency (pyplot needs a canvas already instantiated (actually a manager that holds a canvas) to store into its global registry).
1 parent 9d12377 commit ce951b1

8 files changed

+19
-9
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1706,10 +1706,13 @@ def supports_blit(cls):
17061706
return (hasattr(cls, "copy_from_bbox")
17071707
and hasattr(cls, "restore_region"))
17081708

1709-
def __init__(self, figure):
1709+
def __init__(self, figure=None):
1710+
from matplotlib.figure import Figure
17101711
self._fix_ipython_backend2gui()
17111712
self._is_idle_drawing = True
17121713
self._is_saving = False
1714+
if figure is None:
1715+
figure = Figure()
17131716
figure.set_canvas(self)
17141717
self.figure = figure
17151718
self.manager = None

lib/matplotlib/backends/_backend_tk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class FigureCanvasTk(FigureCanvasBase):
162162
@_api.delete_parameter(
163163
"3.4", "resize_callback",
164164
alternative="get_tk_widget().bind('<Configure>', ..., True)")
165-
def __init__(self, figure, master=None, resize_callback=None):
165+
def __init__(self, figure=None, master=None, resize_callback=None):
166166
super().__init__(figure)
167167
self._idle = True
168168
self._idle_callback = None

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase):
101101
| Gdk.EventMask.POINTER_MOTION_HINT_MASK
102102
| Gdk.EventMask.SCROLL_MASK)
103103

104-
def __init__(self, figure):
104+
def __init__(self, figure=None):
105105
FigureCanvasBase.__init__(self, figure)
106106
GObject.GObject.__init__(self)
107107

lib/matplotlib/backends/backend_qt5.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,13 @@ class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
206206
}
207207

208208
@_allow_super_init
209-
def __init__(self, figure):
209+
def __init__(self, figure=None):
210210
_create_qApp()
211211
super().__init__(figure=figure)
212212

213213
# We don't want to scale up the figure DPI more than once.
214214
# Note, we don't handle a signal for changing DPI yet.
215-
figure._original_dpi = figure.dpi
215+
self.figure._original_dpi = self.figure.dpi
216216
self._update_figure_dpi()
217217
# In cases with mixed resolution displays, we need to be careful if the
218218
# dpi_ratio changes - in this case we need to resize the canvas

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class FigureCanvasQTAgg(FigureCanvasAgg, FigureCanvasQT):
1818

19-
def __init__(self, figure):
19+
def __init__(self, figure=None):
2020
# Must pass 'figure' as kwarg to Qt base class.
2121
super().__init__(figure=figure)
2222

lib/matplotlib/backends/backend_qt5cairo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class FigureCanvasQTCairo(FigureCanvasQT, FigureCanvasCairo):
9-
def __init__(self, figure):
9+
def __init__(self, figure=None):
1010
super().__init__(figure=figure)
1111
self._renderer = RendererCairo(self.figure.dpi)
1212
self._renderer.set_width_height(-1, -1) # Invalid values.

lib/matplotlib/backends/backend_wx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ class _FigureCanvasWxBase(FigureCanvasBase, wx.Panel):
494494
wx.WXK_NUMPAD_DELETE: 'delete',
495495
}
496496

497-
def __init__(self, parent, id, figure):
497+
def __init__(self, parent, id, figure=None):
498498
"""
499499
Initialize a FigureWx instance.
500500
@@ -504,7 +504,7 @@ def __init__(self, parent, id, figure):
504504
"""
505505

506506
FigureCanvasBase.__init__(self, figure)
507-
w, h = map(math.ceil, figure.bbox.size)
507+
w, h = map(math.ceil, self.figure.bbox.size)
508508
# Set preferred window size hint - helps the sizer, if one is connected
509509
wx.Panel.__init__(self, parent, id, size=wx.Size(w, h))
510510
# Create the drawing bitmap

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import os
12
import re
23

34
from matplotlib.backend_bases import (
45
FigureCanvasBase, LocationEvent, MouseButton, MouseEvent,
56
NavigationToolbar2, RendererBase)
67
from matplotlib.backend_tools import (ToolZoom, ToolPan, RubberbandBase,
78
ToolViewsPositions, _views_positions)
9+
from matplotlib.figure import Figure
810
import matplotlib.pyplot as plt
911
import matplotlib.transforms as transforms
1012
import matplotlib.path as path
13+
1114
import numpy as np
1215
import pytest
1316

@@ -53,6 +56,10 @@ def check(master_transform, paths, all_transforms,
5356
check(id, paths, tforms_matrices, offsets, facecolors[0:1], edgecolors)
5457

5558

59+
def test_canvas_ctor():
60+
assert isinstance(FigureCanvasBase().figure, Figure)
61+
62+
5663
def test_get_default_filename(tmpdir):
5764
plt.rcParams['savefig.directory'] = str(tmpdir)
5865
fig = plt.figure()

0 commit comments

Comments
 (0)