Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 698b259

Browse filesBrowse 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 a6d684f commit 698b259
Copy full SHA for 698b259

File tree

Expand file treeCollapse file tree

8 files changed

+19
-10
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+19
-10
lines changed

‎lib/matplotlib/backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_bases.py
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,10 +1711,13 @@ def supports_blit(cls):
17111711
return (hasattr(cls, "copy_from_bbox")
17121712
and hasattr(cls, "restore_region"))
17131713

1714-
def __init__(self, figure):
1714+
def __init__(self, figure=None):
1715+
from matplotlib.figure import Figure
17151716
self._fix_ipython_backend2gui()
17161717
self._is_idle_drawing = True
17171718
self._is_saving = False
1719+
if figure is None:
1720+
figure = Figure()
17181721
figure.set_canvas(self)
17191722
self.figure = figure
17201723
self.manager = None

‎lib/matplotlib/backends/_backend_tk.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/_backend_tk.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _on_timer(self):
113113
class FigureCanvasTk(FigureCanvasBase):
114114
required_interactive_framework = "tk"
115115

116-
def __init__(self, figure, master=None, resize_callback=None):
116+
def __init__(self, figure=None, master=None, resize_callback=None):
117117
super().__init__(figure)
118118
self._idle = True
119119
self._idle_callback = None

‎lib/matplotlib/backends/backend_gtk3.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_gtk3.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_qt5.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,13 @@ class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
212212
}
213213

214214
@_allow_super_init
215-
def __init__(self, figure):
215+
def __init__(self, figure=None):
216216
_create_qApp()
217217
super().__init__(figure=figure)
218218

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

‎lib/matplotlib/backends/backend_qt5agg.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_qt5agg.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_qt5cairo.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_wx.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ class _FigureCanvasWxBase(FigureCanvasBase, wx.Panel):
493493
wx.WXK_NUMPAD_DELETE: 'delete',
494494
}
495495

496-
def __init__(self, parent, id, figure):
496+
def __init__(self, parent, id, figure=None):
497497
"""
498498
Initialize a FigureWx instance.
499499
@@ -503,7 +503,7 @@ def __init__(self, parent, id, figure):
503503
"""
504504

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

‎lib/matplotlib/tests/test_backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backend_bases.py
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +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
11-
import os
13+
1214
import numpy as np
1315
import pytest
1416

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

5658

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.