8000 Added doc and cleaned backend_managers, don't want our new file dirty. · matplotlib/matplotlib@a0d1110 · GitHub
[go: up one dir, main page]

Skip to content

Commit a0d1110

Browse files
committed
Added doc and cleaned backend_managers, don't want our new file dirty.
1 parent 4df6923 commit a0d1110

File tree

7 files changed

+154
-46
lines changed

7 files changed

+154
-46
lines changed

doc/api/backend_managers_api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
:mod:`matplotlib.backend_managers`
3+
==================================
4+
5+
.. automodule:: matplotlib.backend_managers
6+
:members:
7+
:undoc-members:
8+
:show-inheritance:

doc/api/index_backend_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ backends
55
.. toctree::
66

77
backend_bases_api.rst
8+
backend_managers_api.rst
89
backend_gtkagg_api.rst
910
backend_qt4agg_api.rst
1011
backend_wxagg_api.rst

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: 74 additions & 7 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
@@ -2503,10 +2509,10 @@ def key_press_handler(event, canvas, toolbar=None):
25032509

25042510
# quit the figure (defaut key 'ctrl+w')
25052511
if event.key in quit_keys:
2506-
if isinstance(canvas.manager.mainloop, MainLoopBase): # If new no Gcf.
2507-
canvas.manager._destroy('window_destroy_event')
2508-
else:
2512+
if isinstance(canvas.manager, FigureManagerBase): # Using old figman.
25092513
Gcf.destroy_fig(canvas.figure)
2514+
else:
2515+
canvas.manager._destroy('window_destroy_event')
25102516

25112517
if toolbar is not None:
25122518
# home or reset mnemonic (default key 'h', 'home' and 'r')
@@ -2589,6 +2595,14 @@ def __init__(self, name, window):
25892595

25902596

25912597
class WindowBase(cbook.EventEmitter):
2598+
"""The base class to show a window on screen.
2599+
2600+
Parameters
2601+
----------
2602+
title : str
2603+
The title of the window.
2604+
"""
2605+
25922606
def __init__(self, title):
25932607
cbook.EventEmitter.__init__(self)
25942608

@@ -2602,39 +2616,92 @@ def show(self):
26022616
raise NonGuiException()
26032617

26042618
def destroy(self):
2619+
"""Destroys the window"""
26052620
pass
26062621

26072622
def set_fullscreen(self, fullscreen):
2623+
"""Whether to show the window fullscreen or not, GUI only.
2624+
2625+
Parameters
2626+
----------
2627+
fullscreen : bool
2628+
True for yes, False for no.
2629+
"""
26082630
pass
26092631

2610-
def set_default_size(self, w, h):
2632+
def set_default_size(self, width, height):
2633+
"""Sets the default size of the window, defaults to a simple resize.
2634+
2635+
Parameters
2636+
----------
2637+
width : int
2638+
The default width (in pixels) of the window.
2639+
height : int
2640+
The default height (in pixels) of the window.
2641+
"""
26112642
self.resize(w, h)
26122643

2613-
def resize(self, w, h):
2614-
""""For gui backends, resize the window (in pixels)."""
2644+
def resize(self, width, height):
2645+
""""For gui backends, resizes the window.
2646+
2647+
Parameters
2648+
----------
2649+
width : int
2650+
The new width (in pixels) for the window.
2651+
height : int
2652+
The new height (in pixels) for the window.
2653+
"""
26152654
pass
26162655

26172656
def get_window_title(self):
26182657
"""
26192658
Get the title text of the window containing the figure.
26202659
Return None for non-GUI backends (e.g., a PS backend).
2660+
2661+
Returns
2662+
-------
2663+
str : The window's title.
26212664
"""
26222665
return 'image'
26232666

26242667
def set_window_title(self, title):
26252668
"""
26262669
Set the title text of the window containing the figure. Note that
26272670
this has no effect for non-GUI backends (e.g., a PS backend).
2671+
2672+
Parameters
2673+
----------
2674+
title : str
2675+
The title of the window.
26282676
"""
26292677
pass
26302678

26312679
def add_element_to_window(self, element, expand, fill, pad, side='bottom'):
26322680
""" Adds a gui widget to the window.
2633-
This has no effect for non-GUI backends
2681+
This has no effect for non-GUI backends and properties only apply
2682+
to those backends that support them, or have a suitable workaround.
2683+
2684+
Parameters
2685+
----------
2686+
element : A gui element.
2687+
The element to add to the window
2688+
expand : bool
2689+
Whether the element should auto expand to fill as much space within
2690+
the window as possible.
2691+
fill : bool
2692+
If the element can expand, should it make the element bigger,
2693+
or go into extra padding? True, False respectfully.
2694+
pad : int
2695+
The extra amount of space in pixels to pad the element.
26342696
"""
26352697
pass
26362698

26372699
def destroy_event(self, *args):
2700+
"""Fires this event when the window wants to destroy itself.
2701+
2702+
Note this method should hook up to the backend's internal window's
2703+
close event.
2704+
"""
26382705
s = 'window_destroy_event'
26392706
event = WindowEvent(s, self)
26402707
self._callbacks.process(s, event)

lib/matplotlib/backend_managers.py

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,56 @@
99

1010

1111
class FigureManagerEvent(object):
12-
def __init__(self, s, fm):
13-
self.name = s
14-
self.figure_manager = fm
12+
"""Event for when something happens to this figure manager.
13+
i.e. the figure it controls gets closed
14+
15+
Attributes
16+
----------
17+
signal : str
18+
The name of the signal.
19+
20+
figure_manager : FigureManager
21+
The figure manager that fired the event.
22+
"""
23+
def __init__(self, signal, figure_manager):
24+
self.name = signal
25+
self.figure_manager = figure_manager
1526

1627

1728
class FigureManager(cbook.EventEmitter):
29+
"""
30+
The FigureManager creates and wraps the necessary components to display a
31+
figure, namely the Window, FigureCanvas and Toolbar. It gets used whenever
32+
you want the figure in a standalone window.
33+
34+
Parameters
35+
----------
36+
figure : `matplotlib.figure.Figure`
37+
The figure to manage.
38+
39+
num : int
40+
The figure number.
41+
42+
Attributes
43+
----------
44+
45+
canvas : `matplotlib.backend_bases.FigureCanvasBase`
46+
The GUI element on which we draw.
47+
48+
toolbar : `matplotlib.backend_bases.NavigationToolbar2`
49+
The toolbar used for interacting with the figure.
50+
51+
window : `matplotlib.backend_bases.WindowBase`
52+
The window that holds the canvas and toolbar.
53+
54+
num : int
55+
The figure number.
56+
"""
1857
def __init__(self, figure, num):
1958
cbook.EventEmitter.__init__(self)
2059
self.num = num
2160

22-
self.mainloop = MainLoop()
61+
self._mainloop = MainLoop()
2362
self.window = Window('Figure %d' % num)
2463
self.window.mpl_connect('window_destroy_event', self._destroy)
2564

@@ -63,21 +102,28 @@ def _destroy(self, event=None):
63102
self._callbacks.process(s, event)
64103

65104
def destroy(self, *args):
105+
"""Called to destroy this FigureManager, gets called by Gcf through
106+
event magic.
107+
"""
66108
self.canvas.destroy()
67109
if self.toolbar:
68110
self.toolbar.destroy()
69111
self.window.destroy()
70112

71-
self.mainloop.__del__()
113+
self._mainloop.__del__()
72114

73115
def show(self):
116+
"""Shows the figure"""
74117
self.window.show()
75118

76119
def full_screen_toggle(self):
120+
"""Toggles whether we show fullscreen, alternatively call
121+
`window.fullscreen()`"""
77122
self._full_screen_flag = not self._full_screen_flag
78123
self.window.set_fullscreen(self._full_screen_flag)
79124

80125
def resize(self, w, h):
126+
""""For gui backends, resize the window (in pixels)."""
81127
self.window.resize(w, h)
82128

83129
def get_window_title(self):
@@ -108,23 +154,3 @@ def show_popup(self, msg):
108154
Display message in a popup -- GUI only
109155
"""
110156
pass
111-
112-
113-
def new_figure_manager(num, *args, **kwargs):
114-
"""
115-
Create a new figure manager instance
116-
"""
117-
show = kwargs.pop('show', None)
118-
if old_new_figure_manager is None: # Test if we can use the new code
119-
FigureClass = kwargs.pop('FigureClass', Figure)
120-
thisFig = FigureClass(*args, **kwargs)
121-
manager = new_figure_manager_given_figure(num, thisFig)
122-
else: # TODO remove once Gcf removed from backends. Default to old code.
123-
manager = old_new_figure_manager(num, *args, **kwargs)
124-
manager.mainloop = MainLoop
125-
return manager
126-
127-
128-
def new_figure_manager_given_figure(num, figure):
129-
manager = FigureManager(figure, num)
130-
return manager

lib/matplotlib/backends/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ def get_backends():
2323
_temp = __import__(backend_name, globals(), locals(),
2424
['Window', 'Toolbar2', 'FigureCanvas', 'MainLoop',
2525
'new_figure_manager'], 0)
26-
FigureCanvas = _temp.FigureCanvas
2726
try:
2827
Window = _temp.Window
2928
Toolbar2 = _temp.Toolbar2
29+
FigureCanvas = _temp.FigureCanvas
3030
MainLoop = _temp.MainLoop
3131
old_new_figure_manager = None
3232
except AttributeError:
3333
Window = None
3434
Toolbar2 = None
35+
FigureCanvas = None
3536
MainLoop = getattr(_temp, 'show', do_nothing_show)
3637
old_new_figure_manager = _temp.new_figure_manager
3738

@@ -54,7 +55,7 @@ def pylab_setup():
5455

5556
def do_nothing(*args, **kwargs): pass
5657
backend_version = getattr(backend_mod,'backend_version', 'unknown')
57-
show = None if hasattr(backend_mod, 'show') else do_nothing_show
58+
show = getattr(backend_mod, 'show', do_nothing_show)
5859
draw_if_interactive = getattr(backend_mod, 'draw_if_interactive', do_nothing)
5960

6061
# Additional imports which only happen for certain backends. This section

lib/matplotlib/pyplot.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ def show(*args, **kw):
152152
described above.
153153
"""
154154
global _show
155-
if _show is None:
155+
if backend_managers.Window is not None: # Can we use the new code?
156156
return _pylab_helpers.Gcf.show_all(*args, **kw)
157157
else:
158-
_show(*args, **kw)
158+
_show(*args, **kw)
159159

160160

161161
def isinteractive():
@@ -429,14 +429,18 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
429429
if get_backend().lower() == 'ps':
430430
dpi = 72
431431

432-
figManager = backend_managers.new_figure_manager(num, figsize=figsize,
433-
dpi=dpi,
434-
facecolor=facecolor,
435-
edgecolor=edgecolor,
436-
frameon=frameon,
437-
FigureClass=FigureClass,
438-
show=_show,
439-
**kwargs)
432+
if backend_managers.Window is not None: # Can we use the new code?
433+
fig = FigureClass(figsize=figsize, dpi=dpi, facecolor=facecolor,
434+
edgecolor=edgecolor, frameon=frameon, **kwargs)
435+
figManager = backend_managers.FigureManager(fig, num)
436+
else:
437+
figManager = new_figure_manager(num, figsize=figsize,
438+
dpi=dpi,
439+
facecolor=facecolor,
440+
edgecolor=edgecolor,
441+
frameon=frameon,
442+
FigureClass=FigureClass,
443+
**kwargs)
440444

441445
if figLabel:
442446
figManager.set_window_title(figLabel)

0 commit comments

Comments
 (0)
0