Skip to content

Commit 1a47d3b

Browse files
OceanWolftacaswell
authored andcommitted
Added doc and cleaned backend_managers, don't want our new file dirty.
1 parent 6ae52f2 commit 1a47d3b

File tree

6 files changed

+152
-58
lines changed

6 files changed

+152
-58
lines changed

lib/matplotlib/_pylab_helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def show_all(cls, block=None):
6868
it does not block if run inside ipython's "%pylab" mode
6969
it does not block in interactive mode.
7070
"""
71+
7172
managers = cls.get_all_fig_managers()
7273
if not managers:
7374
return
@@ -77,7 +78,7 @@ def show_all(cls, block=None):
7778

7879
if block is not None:
7980
if block:
80-
manager.mainloop()
81+
manager._mainloop()
8182
return
8283

8384
from matplotlib import pyplot
@@ -99,7 +100,7 @@ def show_all(cls, block=None):
99100
block = False
100101

101102
if not is_interactive() or get_backend() == 'WebAgg':
102-
manager.mainloop()
103+
manager._mainloop()
103104

104105
@classmethod
105106
def destroy(cls, num):

lib/matplotlib/backend_bases.py

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
pressed, x and y locations in pixel and
2121
:class:`~matplotlib.axes.Axes` coordinates.
2222
23+
:class:`WindowBase`
24+
The base class to display a window.
25+
26+
:class:`MainLoopBase`
27+
The base class to start the GUI's main loop.
28+
2329
:class:`ShowBase`
2430
The base class for the Show class of each interactive backend;
2531
the 'show' callable is then set to Show.__call__, inherited from
@@ -2524,10 +2530,10 @@ def key_press_handler(event, canvas, toolbar=None):
25242530

25252531
# quit the figure (defaut key 'ctrl+w')
25262532
if event.key in quit_keys:
2527-
if isinstance(canvas.manager.mainloop, MainLoopBase): # If new no Gcf.
2528-
canvas.manager._destroy('window_destroy_event')
2529-
else:
2533+
if isinstance(canvas.manager, FigureManagerBase): # Using old figman.
25302534
Gcf.destroy_fig(canvas.figure)
2535+
else:
2536+
canvas.manager._destroy('window_destroy_event')
25312537

25322538
if toolbar is not None:
25332539
# home or reset mnemonic (default key 'h', 'home' and 'r')
@@ -2662,6 +2668,14 @@ def __init__(self, name, window):
26622668

26632669

26642670
class WindowBase(cbook.EventEmitter):
2671+
"""The base class to show a window on screen.
2672+
2673+
Parameters
2674+
----------
2675+
title : str
2676+
The title of the window.
2677+
"""
2678+
26652679
def __init__(self, title):
26662680
cbook.EventEmitter.__init__(self)
26672681

@@ -2675,39 +2689,92 @@ def show(self):
26752689
raise NonGuiException()
26762690

26772691
def destroy(self):
2692+
"""Destroys the window"""
26782693
pass
26792694

26802695
def set_fullscreen(self, fullscreen):
2696+
"""Whether to show the window fullscreen or not, GUI only.
2697+
2698+
Parameters
2699+
----------
2700+
fullscreen : bool
2701+
True for yes, False for no.
2702+
"""
26812703
pass
26822704

2683-
def set_default_size(self, w, h):
2684-
self.resize(w, h)
2705+
def set_default_size(self, width, height):
2706+
"""Sets the default size of the window, defaults to a simple resize.
26852707
2686-
def resize(self, w, h):
2687-
""""For gui backends, resize the window (in pixels)."""
2708+
Parameters
2709+
----------
2710+
width : int
2711+
The default width (in pixels) of the window.
2712+
height : int
2713+
The default height (in pixels) of the window.
2714+
"""
2715+
self.resize(width, height)
2716+
2717+
def resize(self, width, height):
2718+
""""For gui backends, resizes the window.
2719+
2720+
Parameters
2721+
----------
2722+
width : int
2723+
The new width (in pixels) for the window.
2724+
height : int
2725+
The new height (in pixels) for the window.
2726+
"""
26882727
pass
26892728

26902729
def get_window_title(self):
26912730
"""
26922731
Get the title text of the window containing the figure.
26932732
Return None for non-GUI backends (e.g., a PS backend).
2733+
2734+
Returns
2735+
-------
2736+
str : The window's title.
26942737
"""
26952738
return 'image'
26962739

26972740
def set_window_title(self, title):
26982741
"""
26992742
Set the title text of the window containing the figure. Note that
27002743
this has no effect for non-GUI backends (e.g., a PS backend).
2744+
2745+
Parameters
2746+
----------
2747+
title : str
2748+
The title of the window.
27012749
"""
27022750
pass
27032751

27042752
def add_element_to_window(self, element, expand, fill, pad, side='bottom'):
27052753
""" Adds a gui widget to the window.
2706-
This has no effect for non-GUI backends
2754+
This has no effect for non-GUI backends and properties only apply
2755+
to those backends that support them, or have a suitable workaround.
2756+
2757+
Parameters
2758+
----------
2759+
element : A gui element.
2760+
The element to add to the window
2761+
expand : bool
2762+
Whether the element should auto expand to fill as much space within
2763+
the window as possible.
2764+
fill : bool
2765+
If the element can expand, should it make the element bigger,
2766+
or go into extra padding? True, False respectfully.
2767+
pad : int
2768+
The extra amount of space in pixels to pad the element.
27072769
"""
27082770
pass
27092771

27102772
def destroy_event(self, *args):
2773+
"""Fires this event when the window wants to destroy itself.
2774+
2775+
Note this method should hook up to the backend's internal window's
2776+
close event.
2777+
"""
27112778
s = 'window_destroy_event'
27122779
event = WindowEvent(s, self)
27132780
self._callbacks.process(s, event)

lib/matplotlib/backend_managers.py

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,60 @@
1919
from matplotlib.figure import Figure
2020
from matplotlib.backend_bases import key_press_handler
2121
from matplotlib.backends import get_backends
22-
(FigureCanvas, Window, Toolbar2, MainLoop,
23-
old_new_figure_manager) = get_backends()
22+
FigureCanvas, Window, Toolbar2, MainLoop = get_backends()
2423

2524

2625
class FigureManagerEvent(object):
27-
def __init__(self, s, fm):
28-
self.name = s
29-
self.figure_manager = fm
26+
"""Event for when something happens to this figure manager.
27+
i.e. the figure it controls gets closed
28+
29+
Attributes
30+
----------
31+
signal : str
32+
The name of the signal.
33+
34+
figure_manager : FigureManager
35+
The figure manager that fired the event.
36+
"""
37+
def __init__(self, signal, figure_manager):
38+
self.name = signal
39+
self.figure_manager = figure_manager
3040

3141

3242
class FigureManager(cbook.EventEmitter):
43+
"""
44+
The FigureManager creates and wraps the necessary components to display a
45+
figure, namely the Window, FigureCanvas and Toolbar. It gets used whenever
46+
you want the figure in a standalone window.
47+
48+
Parameters
49+
----------
50+
figure : `matplotlib.figure.Figure`
51+
The figure to manage.
52+
53+
num : int
54+
The figure number.
55+
56+
Attributes
57+
----------
58+
59+
canvas : `matplotlib.backend_bases.FigureCanvasBase`
60+
The GUI element on which we draw.
61+
62+
toolbar : `matplotlib.backend_bases.NavigationToolbar2`
63+
The toolbar used for interacting with the figure.
64+
65+
window : `matplotlib.backend_bases.WindowBase`
66+
The window that holds the canvas and toolbar.
67+
68+
num : int
69+
The figure number.
70+
"""
3371
def __init__(self, figure, num):
3472
cbook.EventEmitter.__init__(self)
3573
self.num = num
3674

37-
self.mainloop = MainLoop()
75+
self._mainloop = MainLoop()
3876
self.window = Window('Figure %d' % num)
3977
self.window.mpl_connect('window_destroy_event', self._destroy)
4078

@@ -78,21 +116,28 @@ def _destroy(self, event=None):
78116
self._callbacks.process(s, event)
79117

80118
def destroy(self, *args):
119+
"""Called to destroy this FigureManager, gets called by Gcf through
120+
event magic.
121+
"""
81122
self.canvas.destroy()
82123
if self.toolbar:
83124
self.toolbar.destroy()
84125
self.window.destroy()
85126

86-
self.mainloop.__del__()
127+
self._mainloop.__del__()
87128

88129
def show(self):
130+
"""Shows the figure"""
89131
self.window.show()
90132

91133
def full_screen_toggle(self):
134+
"""Toggles whether we show fullscreen, alternatively call
135+
`window.fullscreen()`"""
92136
self._full_screen_flag = not self._full_screen_flag
93137
self.window.set_fullscreen(self._full_screen_flag)
94138

95139
def resize(self, w, h):
140+
""""For gui backends, resize the window (in pixels)."""
96141
self.window.resize(w, h)
97142

98143
def get_window_title(self):
@@ -544,23 +589,3 @@ def get_tool(self, name, warn=True):
544589
warnings.warn("ToolManager does not control tool %s" % name)
545590
return None
546591
return self._tools[name]
547-
548-
549-
def new_figure_manager(num, *args, **kwargs):
550-
"""
551-
Create a new figure manager instance
552-
"""
553-
show = kwargs.pop('show', None)
554-
if old_new_figure_manager is None: # Test if we can use the new code
555-
FigureClass = kwargs.pop('FigureClass', Figure)
556-
thisFig = FigureClass(*args, **kwargs)
557-
manager = new_figure_manager_given_figure(num, thisFig)
558-
else: # TODO remove once Gcf removed from backends. Default to old code.
559-
manager = old_new_figure_manager(num, *args, **kwargs)
560-
manager.mainloop = MainLoop
561-
return manager
562-
563-
564-
def new_figure_manager_given_figure(num, figure):
565-
manager = FigureManager(figure, num)
566-
return manager

lib/matplotlib/backends/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,19 @@ def get_backend_name(name=None):
3939
def get_backends():
4040
backend_name = get_backend_name()
4141
_temp = __import__(backend_name, globals(), locals(),
42-
['Window', 'Toolbar2', 'FigureCanvas', 'MainLoop',
43-
'new_figure_manager'], 0)
44-
FigureCanvas = _temp.FigureCanvas
42+
['Window', 'Toolbar2', 'FigureCanvas', 'MainLoop'], 0)
4543
try:
4644
Window = _temp.Window
4745
Toolbar2 = _temp.Toolbar2
46+
FigureCanvas = _temp.FigureCanvas
4847
MainLoop = _temp.MainLoop
49-
old_new_figure_manager = None
5048
except AttributeError:
5149
Window = None
5250
Toolbar2 = None
51+
FigureCanvas = None
5352
MainLoop = getattr(_temp, 'show', do_nothing_show)
54-
old_new_figure_manager = _temp.new_figure_manager
5553

56-
return FigureCanvas, Window, Toolbar2, MainLoop, old_new_figure_manager
54+
return FigureCanvas, Window, Toolbar2, MainLoop
5755

5856

5957
def pylab_setup(name=None):
@@ -102,7 +100,7 @@ def do_nothing(*args, **kwargs):
102100

103101
backend_version = getattr(backend_mod, 'backend_version', 'unknown')
104102

105-
show = None if hasattr(backend_mod, 'show') else do_nothing_show
103+
show = getattr(backend_mod, 'show', do_nothing_show)
106104

107105
draw_if_interactive = getattr(backend_mod, 'draw_if_interactive',
108106
do_nothing)

lib/matplotlib/figure.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,12 +1649,11 @@ def __setstate__(self, state):
16491649
import matplotlib.backend_managers as managers
16501650
allnums = plt.get_fignums()
16511651
num = max(allnums) + 1 if allnums else 1
1652-
if managers.old_new_figure_manager:
1652+
if managers.Window is not None: # Can we use the new code?
1653+
mgr = managers.FigureManager(self, num)
1654+
else:
16531655
mgr = plt._backend_mod.new_figure_manager_given_figure(num,
16541656
self)
1655-
mgr.mainloop = plt._show
1656-
else:
1657-
mgr = managers.FigureManager(self, num)
16581657

16591658
# XXX The following is a copy and paste from pyplot. Consider
16601659
# factoring to pylab_helpers

lib/matplotlib/pyplot.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ def show(*args, **kw):
250250
described above.
251251
"""
252252
global _show
253-
if _show is None:
253+
if backend_managers.Window is not None: # Can we use the new code?
254254
return _pylab_helpers.Gcf.show_all(*args, **kw)
255255
else:
256-
_show(*args, **kw)
256+
_show(*args, **kw)
257257

258258

259259
def isinteractive():
@@ -539,14 +539,18 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
539539
if get_backend().lower() == 'ps':
540540
dpi = 72
541541

542-
figManager = backend_managers.new_figure_manager(num, figsize=figsize,
543-
dpi=dpi,
544-
facecolor=facecolor,
545-
edgecolor=edgecolor,
546-
frameon=frameon,
547-
FigureClass=FigureClass,
548-
show=_show,
549-
**kwargs)
542+
if backend_managers.Window is not None: # Can we use the new code?
543+
fig = FigureClass(figsize=figsize, dpi=dpi, facecolor=facecolor,
544+
edgecolor=edgecolor, frameon=frameon, **kwargs)
545+
figManager = backend_managers.FigureManager(fig, num)
546+
else:
547+
figManager = new_figure_manager(num, figsize=figsize,
548+
dpi=dpi,
549+
facecolor=facecolor,
550+
edgecolor=edgecolor,
551+
frameon=frameon,
552+
FigureClass=FigureClass,
553+
**kwargs)
550554

551555
if figLabel:
552556
figManager.set_window_title(figLabel)

0 commit comments

Comments
 (0)