Skip to content

Commit 5dbc280

Browse files
committed
Merged revisions 8559,8562 via svnmerge from
https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v1_0_maint ........ r8559 | ianthomas23 | 2010-07-16 03:46:14 -1000 (Fri, 16 Jul 2010) | 2 lines Added tri* functions to pyplot docs. ........ r8562 | efiring | 2010-07-16 10:44:52 -1000 (Fri, 16 Jul 2010) | 3 lines backends: factored out most of the show() code into ShowBase class. Also fixed various fltkagg problems. ........ svn path=/trunk/matplotlib/; revision=8563
1 parent 2135d78 commit 5dbc280

9 files changed

+126
-119
lines changed

lib/matplotlib/backend_bases.py

+37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
pressed, x and y locations in pixel and
2222
:class:`~matplotlib.axes.Axes` coordinates.
2323
24+
:class:`ShowBase`
25+
The base class for the Show class of each interactive backend;
26+
the 'show' callable is then set to Show.__call__, inherited from
27+
ShowBase.
28+
2429
"""
2530

2631
from __future__ import division
@@ -33,6 +38,8 @@
3338
import matplotlib.widgets as widgets
3439
#import matplotlib.path as path
3540
from matplotlib import rcParams
41+
from matplotlib import is_interactive
42+
from matplotlib._pylab_helpers import Gcf
3643

3744
from matplotlib.transforms import Bbox, TransformedBbox, Affine2D
3845
import cStringIO
@@ -53,6 +60,36 @@ def register_backend(format, backend_class):
5360
_backend_d[format] = backend_class
5461

5562

63+
class ShowBase(object):
64+
"""
65+
Simple base class to generate a show() callable in backends.
66+
67+
Subclass must override mainloop() method.
68+
"""
69+
def __call__(self):
70+
"""
71+
Show all figures.
72+
"""
73+
managers = Gcf.get_all_fig_managers()
74+
if not managers:
75+
return
76+
77+
for manager in managers:
78+
manager.show()
79+
80+
try:
81+
if not self._needmain: # ipython flag
82+
return
83+
except AttributeError:
84+
pass
85+
86+
if not is_interactive():
87+
self.mainloop()
88+
89+
def mainloop(self):
90+
pass
91+
92+
5693

5794
class RendererBase:
5895
"""An abstract base class to handle drawing/rendering operations.

lib/matplotlib/backends/backend_cocoaagg.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import matplotlib
3131
from matplotlib.figure import Figure
3232
from matplotlib.backend_bases import FigureManagerBase, FigureCanvasBase
33+
from matplotlib.backend_bases import ShowBase
34+
3335
from backend_agg import FigureCanvasAgg
3436
from matplotlib._pylab_helpers import Gcf
3537

@@ -41,9 +43,23 @@ def new_figure_manager(num, *args, **kwargs):
4143
canvas = FigureCanvasCocoaAgg(thisFig)
4244
return FigureManagerCocoaAgg(canvas, num)
4345

44-
def show():
45-
for manager in Gcf.get_all_fig_managers():
46-
manager.show()
46+
## Below is the original show() function:
47+
#def show():
48+
# for manager in Gcf.get_all_fig_managers():
49+
# manager.show()
50+
#
51+
## It appears that this backend is unusual in having a separate
52+
## run function invoked for each figure, instead of a single
53+
## mainloop. Presumably there is no blocking at all.
54+
##
55+
## Using the Show class below should cause no difference in
56+
## behavior.
57+
58+
class Show(ShowBase):
59+
def mainloop(self):
60+
pass
61+
62+
show = Show()
4763

4864
def draw_if_interactive():
4965
if matplotlib.is_interactive():

lib/matplotlib/backends/backend_fltkagg.py

+14-35
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,14 @@
2424
from matplotlib.backend_bases import \
2525
RendererBase, GraphicsContextBase, FigureManagerBase, FigureCanvasBase,\
2626
NavigationToolbar2, cursors
27+
from matplotlib.backend_bases import ShowBase
28+
29+
2730
from matplotlib.figure import Figure
2831
from matplotlib._pylab_helpers import Gcf
2932
import matplotlib.backends.windowing as windowing
3033
from matplotlib.widgets import SubplotTool
3134

32-
33-
import thread,time
34-
35-
Fl_running=thread.allocate_lock()
36-
def Fltk_run_interactive():
37-
global Fl_running
38-
if Fl_running.acquire(0):
39-
while True:
40-
Fltk.Fl.check()
41-
time.sleep(0.005)
42-
else:
43-
print "fl loop already running"
44-
4535
# the true dots per inch on the screen; should be display dependent
4636
# see http://groups.google.com/groups?q=screen+dpi+x11&hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=7077.26e81ad5%40swift.cs.tcd.ie&rnum=5 for some info about screen dpi
4737
PIXELS_PER_INCH = 75
@@ -75,29 +65,12 @@ def draw_if_interactive():
7565
if figManager is not None:
7666
figManager.canvas.draw()
7767

68+
class Show(ShowBase):
69+
def mainloop(self):
70+
Fltk.Fl.run()
7871

79-
def ishow():
80-
"""
81-
Show all the figures and enter the fltk mainloop in another thread
82-
This allows to keep hand in interractive python session
83-
Warning: does not work under windows
84-
This should be the last line of your script
85-
"""
86-
for manager in Gcf.get_all_fig_managers():
87-
manager.show()
88-
if show._needmain:
89-
thread.start_new_thread(Fltk_run_interactive,())
90-
show._needmain = False
72+
show = Show()
9173

92-
def show():
93-
"""
94-
Show all the figures and enter the fltk mainloop
95-
96-
This should be the last line of your script
97-
"""
98-
for manager in Gcf.get_all_fig_managers():
99-
manager.show()
100-
Fltk.Fl.run()
10174

10275
def new_figure_manager(num, *args, **kwargs):
10376
"""
@@ -249,8 +222,9 @@ def stop_event_loop(self):
249222
FigureCanvasBase.stop_event_loop_default(self)
250223
stop_event_loop.__doc__=FigureCanvasBase.stop_event_loop_default.__doc__
251224

252-
def destroy_figure(ptr,figman):
225+
def destroy_figure(ptr, figman):
253226
figman.window.hide()
227+
Fltk.Fl.wait(0) # This is needed to make the last figure vanish.
254228
Gcf.destroy(figman._num)
255229

256230
class FigureManagerFltkAgg(FigureManagerBase):
@@ -301,6 +275,11 @@ def show(self):
301275
self.canvas.draw()
302276
self.window.redraw()
303277

278+
def destroy(self):
279+
self.window.hide()
280+
Fltk.Fl.wait(0) # This is needed to make the last figure vanish.
281+
Gcf.destroy(self._num)
282+
304283
def set_window_title(self, title):
305284
self.window_title=title
306285
self.window.label(title)

lib/matplotlib/backends/backend_gtk.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def fn_name(): return sys._getframe(1).f_code.co_name
2020
_new_tooltip_api = (gtk.pygtk_version[1] >= 12)
2121

2222
import matplotlib
23-
from matplotlib import verbose
2423
from matplotlib._pylab_helpers import Gcf
2524
from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \
2625
FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors, TimerBase
26+
from matplotlib.backend_bases import ShowBase
27+
2728
from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK
2829
from matplotlib.cbook import is_string_like, is_writable_file_like
2930
from matplotlib.colors import colorConverter
@@ -65,17 +66,12 @@ def draw_if_interactive():
6566
figManager.canvas.draw_idle()
6667

6768

68-
def show(mainloop=True):
69-
"""
70-
Show all the figures and enter the gtk main loop
71-
This should be the last line of your script
72-
"""
73-
for manager in Gcf.get_all_fig_managers():
74-
manager.window.show()
69+
class Show(ShowBase):
70+
def mainloop(self):
71+
if gtk.main_level() == 0:
72+
gtk.main()
7573

76-
if mainloop and gtk.main_level() == 0 and \
77-
len(Gcf.get_all_fig_managers())>0:
78-
gtk.main()
74+
show = Show()
7975

8076
def new_figure_manager(num, *args, **kwargs):
8177
"""

lib/matplotlib/backends/backend_macosx.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from matplotlib._pylab_helpers import Gcf
88
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
99
FigureManagerBase, FigureCanvasBase, NavigationToolbar2
10+
from matplotlib.backend_bases import ShowBase
11+
1012
from matplotlib.cbook import maxdict
1113
from matplotlib.figure import Figure
1214
from matplotlib.path import Path
@@ -20,19 +22,16 @@
2022
import matplotlib
2123
from matplotlib.backends import _macosx
2224

23-
def show():
24-
"""Show all the figures and enter the Cocoa mainloop.
25-
This function will not return until all windows are closed or
26-
the interpreter exits."""
27-
# Having a Python-level function "show" wrapping the built-in
28-
# function "show" in the _macosx extension module allows us to
29-
# to add attributes to "show". This is something ipython does.
30-
_macosx.show()
25+
class Show(ShowBase):
26+
def mainloop(self):
27+
_macosx.show()
28+
29+
show = Show()
3130

3231
class RendererMac(RendererBase):
3332
"""
3433
The renderer handles drawing/rendering operations. Most of the renderer's
35-
methods forwards the command to the renderer's graphics context. The
34+
methods forward the command to the renderer's graphics context. The
3635
renderer does not wrap a C object and is written in pure Python.
3736
"""
3837

lib/matplotlib/backends/backend_qt.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from matplotlib.cbook import is_string_like, onetrue
99
from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \
1010
FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors
11+
from matplotlib.backend_bases import ShowBase
12+
1113
from matplotlib._pylab_helpers import Gcf
1214
from matplotlib.figure import Figure
1315
from matplotlib.mathtext import MathTextParser
@@ -54,22 +56,12 @@ def _create_qApp():
5456

5557
_create_qApp.qAppCreatedHere = False
5658

57-
def show():
58-
"""
59-
Show all the figures and enter the qt main loop
60-
This should be the last line of your script
61-
"""
62-
for manager in Gcf.get_all_fig_managers():
63-
manager.window.show()
64-
65-
if DEBUG: print 'Inside show'
59+
class Show(ShowBase):
60+
def mainloop(self):
61+
if _create_qApp.qAppCreatedHere:
62+
qt.qApp.exec_loop()
6663

67-
figManager = Gcf.get_active()
68-
if figManager != None:
69-
figManager.canvas.draw()
70-
71-
if _create_qApp.qAppCreatedHere:
72-
qt.qApp.exec_loop()
64+
show = Show()
7365

7466

7567
def new_figure_manager( num, *args, **kwargs ):
@@ -281,6 +273,9 @@ def resize(self, width, height):
281273
'set the canvas size in pixels'
282274
self.window.resize(width, height)
283275

276+
def show(self):
277+
self.window.show()
278+
284279
def destroy( self, *args ):
285280
if self.window._destroying: return
286281
self.window._destroying = True
@@ -359,6 +354,7 @@ def _init_toolbar( self ):
359354
# reference holder for subplots_adjust window
360355
self.adj_window = None
361356

357+
362358
def destroy( self ):
363359
for text, tooltip_text, image_file, callback in self.toolitems:
364360
if text is not None:

lib/matplotlib/backends/backend_qt4.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \
1010
FigureManagerBase, FigureCanvasBase, NavigationToolbar2, IdleEvent, \
1111
cursors, TimerBase
12+
from matplotlib.backend_bases import ShowBase
13+
1214
from matplotlib._pylab_helpers import Gcf
1315
from matplotlib.figure import Figure
1416
from matplotlib.mathtext import MathTextParser
@@ -56,22 +58,12 @@ def _create_qApp():
5658

5759
_create_qApp.qAppCreatedHere = False
5860

59-
def show():
60-
"""
61-
Show all the figures and enter the qt main loop
62-
This should be the last line of your script
63-
"""
64-
for manager in Gcf.get_all_fig_managers():
65-
manager.window.show()
66-
67-
if DEBUG: print 'Inside show'
68-
69-
figManager = Gcf.get_active()
70-
if figManager != None:
71-
figManager.canvas.draw()
61+
class Show(ShowBase):
62+
def mainloop(self):
63+
if _create_qApp.qAppCreatedHere:
64+
QtGui.qApp.exec_()
7265

73-
if _create_qApp.qAppCreatedHere:
74-
QtGui.qApp.exec_()
66+
show = Show()
7567

7668

7769
def new_figure_manager( num, *args, **kwargs ):
@@ -370,6 +362,9 @@ def resize(self, width, height):
370362
'set the canvas size in pixels'
371363
self.window.resize(width, height)
372364

365+
def show(self):
366+
self.window.show()
367+
373368
def destroy( self, *args ):
374369
if self.window._destroying: return
375370
self.window._destroying = True

lib/matplotlib/backends/backend_tkagg.py

+6-15
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
from matplotlib.backend_bases import RendererBase, GraphicsContextBase
1919
from matplotlib.backend_bases import FigureManagerBase, FigureCanvasBase
2020
from matplotlib.backend_bases import NavigationToolbar2, cursors, TimerBase
21+
from matplotlib.backend_bases import ShowBase
22+
from matplotlib._pylab_helpers import Gcf
2123

2224
from matplotlib.figure import Figure
23-
from matplotlib._pylab_helpers import Gcf
2425

2526
from matplotlib.widgets import SubplotTool
2627

@@ -63,22 +64,12 @@ def draw_if_interactive():
6364
if figManager is not None:
6465
figManager.show()
6566

66-
67-
def show():
68-
"""
69-
Show all figures.
70-
71-
"""
72-
for manager in Gcf.get_all_fig_managers():
73-
manager.show()
74-
try:
75-
if not show._needmain: # might have been added by ipython
76-
return
77-
except AttributeError:
78-
pass
79-
if not matplotlib.is_interactive():
67+
class Show(ShowBase):
68+
def mainloop(self):
8069
Tk.mainloop()
8170

71+
show = Show()
72+
8273
def new_figure_manager(num, *args, **kwargs):
8374
"""
8475
Create a new figure manager instance

0 commit comments

Comments
 (0)