Skip to content

Make figure parameter optional when constructing canvases. #18746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class FigureCanvasTk(FigureCanvasBase):
@_api.delete_parameter(
"3.4", "resize_callback",
alternative="get_tk_widget().bind('<Configure>', ..., 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
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_qt5agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_qt5cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down