Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 380c531

Browse files
committed
Attach a FigureCanvasBase by default to Figures.
This is particularly useful for headless cases, where one just wants to take advantage of `print_figure`, which is defined on the base class. For example, one can now do for <loop>: figure = Figure() # do some plotting figure.savefig(...) and let `figure` get gc'd at the next loop iteration; a pyplot-based solution would instead have to explicitly close() the figure or reuse it after clf()'ing it. Also simplifies various places in the codebase where we had to handle the case of `.canvas = None` previously.
1 parent 166d638 commit 380c531

File tree

4 files changed

+17
-27
lines changed

4 files changed

+17
-27
lines changed

lib/matplotlib/artist.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,7 @@ def pickable(self):
449449
--------
450450
set_picker, get_picker, pick
451451
"""
452-
return (self.figure is not None and
453-
self.figure.canvas is not None and
454-
self._picker is not None)
452+
return self.figure is not None and self._picker is not None
455453

456454
def pick(self, mouseevent):
457455
"""

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,8 +3241,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
32413241
if other is not self:
32423242
other.set_xlim(self.viewLim.intervalx,
32433243
emit=False, auto=auto)
3244-
if (other.figure != self.figure and
3245-
other.figure.canvas is not None):
3244+
if other.figure != self.figure:
32463245
other.figure.canvas.draw_idle()
32473246
self.stale = True
32483247
return left, right
@@ -3630,8 +3629,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
36303629
if other is not self:
36313630
other.set_ylim(self.viewLim.intervaly,
36323631
emit=False, auto=auto)
3633-
if (other.figure != self.figure and
3634-
other.figure.canvas is not None):
3632+
if other.figure != self.figure:
36353633
other.figure.canvas.draw_idle()
36363634
self.stale = True
36373635
return bottom, top

lib/matplotlib/figure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import matplotlib.artist as martist
2525
from matplotlib.artist import Artist, allow_rasterization
26+
from matplotlib.backend_bases import FigureCanvasBase
2627
import matplotlib.cbook as cbook
2728
import matplotlib.colorbar as cbar
2829
import matplotlib.image as mimage
@@ -364,7 +365,7 @@ def __init__(self,
364365
self._set_artist_props(self.patch)
365366
self.patch.set_antialiased(False)
366367

367-
self.canvas = None
368+
FigureCanvasBase(self) # Set self.canvas.
368369
self._suptitle = None
369370

370371
if subplotpars is None:
@@ -398,8 +399,7 @@ def __init__(self,
398399
def _repr_html_(self):
399400
# We can't use "isinstance" here, because then we'd end up importing
400401
# webagg unconditiionally.
401-
if (self.canvas is not None and
402-
'WebAgg' in self.canvas.__class__.__name__):
402+
if 'WebAgg' in type(self.canvas).__name__:
403403
from matplotlib.backends import backend_webagg
404404
return backend_webagg.ipython_inline_display(self)
405405

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False,
652652
if other is not self:
653653
other.set_xlim(self.xy_viewLim.intervalx,
654654
emit=False, auto=auto)
655-
if (other.figure != self.figure and
656-
other.figure.canvas is not None):
655+
if other.figure != self.figure:
657656
other.figure.canvas.draw_idle()
658657
self.stale = True
659658
return left, right
@@ -711,8 +710,7 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False,
711710
if other is not self:
712711
other.set_ylim(self.xy_viewLim.intervaly,
713712
emit=False, auto=auto)
714-
if (other.figure != self.figure and
715-
other.figure.canvas is not None):
713+
if other.figure != self.figure:
716714
other.figure.canvas.draw_idle()
717715
self.stale = True
718716
return bottom, top
@@ -770,8 +768,7 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False,
770768
if other is not self:
771769
other.set_zlim(self.zz_viewLim.intervalx,
772770
emit=False, auto=auto)
773-
if (other.figure != self.figure and
774-
other.figure.canvas is not None):
771+
if other.figure != self.figure:
775772
other.figure.canvas.draw_idle()
776773
self.stale = True
777774
return bottom, top
@@ -1070,17 +1067,14 @@ def mouse_init(self, rotate_btn=1, zoom_btn=3):
10701067
10711068
"""
10721069
self.button_pressed = None
1073-
canv = self.figure.canvas
1074-
if canv is not None:
1075-
c1 = canv.mpl_connect('motion_notify_event', self._on_move)
1076-
c2 = canv.mpl_connect('button_press_event', self._button_press)
1077-
c3 = canv.mpl_connect('button_release_event', self._button_release)
1078-
self._cids = [c1, c2, c3]
1079-
else:
1080-
cbook._warn_external("Axes3D.figure.canvas is 'None', mouse "
1081-
"rotation disabled. Set canvas then call "
1082-
"Axes3D.mouse_init().")
1083-
1070+
self._cids = [
1071+
self.figure.canvas.mpl_connect(
1072+
'motion_notify_event', self._on_move),
1073+
self.figure.canvas.mpl_connect(
1074+
'button_press_event', self._button_press),
1075+
self.figure.canvas.mpl_connect(
1076+
'button_release_event', self._button_release),
1077+
]
10841078
# coerce scalars into array-like, then convert into
10851079
# a regular list to avoid comparisons against None
10861080
# which breaks in recent versions of numpy.

0 commit comments

Comments
 (0)