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

Skip to content

Commit cee76c6

Browse files
committed
made tha animation example more uniform. added subplots_adjust button to qt backends
svn path=/trunk/matplotlib/; revision=2627
1 parent 0e90810 commit cee76c6

7 files changed

Lines changed: 101 additions & 26 deletions

File tree

examples/animation_blit.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
ax = p.subplot(111)
1818
canvas = ax.figure.canvas
1919

20+
p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
21+
p.grid() # to ensure proper background restore
22+
2023
# create the initial line
2124
x = nx.arange(0,2*nx.pi,0.01)
2225
line, = p.plot(x, nx.sin(x), animated=True, lw=2)
@@ -33,17 +36,21 @@ def update_line(*args):
3336
# update the data
3437
line.set_ydata(nx.sin(x+update_line.cnt/10.0))
3538
# just draw the animated artist
36-
ax.draw_artist(line)
39+
try:
40+
ax.draw_artist(line)
41+
except AssertionError:
42+
return
3743
# just redraw the axes rectangle
3844
canvas.blit(ax.bbox)
3945

40-
if update_line.cnt==200:
46+
if update_line.cnt==1000:
4147
# print the timing info and quit
42-
print 'FPS:' , 200/(time.time()-tstart)
48+
print 'FPS:' , 1000/(time.time()-tstart)
4349
sys.exit()
4450

4551
update_line.cnt += 1
4652
return True
53+
4754
update_line.cnt = 0
4855
update_line.background = None
4956
gobject.idle_add(update_line)

examples/animation_blit_fltk.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ def update(self,ptr):
3535
# just redraw the axes rectangle
3636
self.canvas.blit(ax.bbox)
3737
self.cnt+=1
38-
if self.cnt==200:
38+
if self.cnt==1000:
3939
# print the timing info and quit
40-
print 'FPS:' , 200/(time.time()-self.tstart)
40+
print 'FPS:' , 1000/(time.time()-self.tstart)
4141
sys.exit()
4242
return True
4343

4444
ax = p.subplot(111)
45+
p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
46+
p.grid() # to ensure proper background restore
4547
# create the initial line
4648
x = nx.arange(0,2*nx.pi,0.01)
4749
line, = p.plot(x, nx.sin(x), animated=True)

examples/animation_blit_qt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
TRUE = 1
1414
FALSE = 0
15-
ITERS = 200
15+
ITERS = 1000
1616

1717
import pylab as p
1818
import matplotlib.numerix as nx

examples/animation_blit_tk.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ def run(*args):
3232
# just redraw the axes rectangle
3333
canvas.blit(ax.bbox)
3434

35-
if run.cnt==200:
35+
if run.cnt==1000:
3636
# print the timing info and quit
37-
print 'FPS:' , 200/(time.time()-tstart)
37+
print 'FPS:' , 1000/(time.time()-tstart)
3838
sys.exit()
3939

4040
run.cnt += 1
4141
run.cnt = 0
4242

4343

44+
p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
45+
p.grid() # to ensure proper background restore
4446
manager = p.get_current_fig_manager()
4547
manager.window.after(100, run)
4648

examples/animation_blit_wx.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
canvas = ax.figure.canvas
2323

2424

25+
p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
26+
p.grid() # to ensure proper background restore
27+
2528
# create the initial line
2629
x = nx.arange(0,2*nx.pi,0.01)
2730
line, = p.plot(x, nx.sin(x), animated=True, lw=2)
@@ -48,18 +51,20 @@ def update_line(*args):
4851
canvas.blit(ax.bbox)
4952
blit_time += time.time() - t
5053

51-
if update_line.cnt==200:
54+
if update_line.cnt==1000:
5255
# print the timing info and quit
5356
frame_time = time.time() - tstart
5457
print '200 frames: %.2f seconds' % frame_time
5558
print '200 blits: %.2f seconds' % blit_time
5659
print
57-
print 'FPS: %.2f' % (200/frame_time)
58-
print 'BPS: %.2f' % (200/blit_time)
60+
print 'FPS: %.2f' % (1000/frame_time)
61+
print 'BPS: %.2f' % (1000/blit_time)
5962
sys.exit()
6063

6164
update_line.cnt += 1
6265
wx.WakeUpIdle()
66+
67+
6368

6469
update_line.cnt = 0
6570
update_line.background = None

lib/matplotlib/backends/backend_qt.py

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from matplotlib._pylab_helpers import Gcf
1616
from matplotlib.figure import Figure
1717
from matplotlib.mathtext import math_parse_s_ft2font
18+
from matplotlib.widgets import SubplotTool
19+
1820
import qt
1921

2022
backend_version = "0.9.1"
@@ -164,22 +166,16 @@ def __init__( self, canvas, num ):
164166
self.window.setCaption( "Figure %d" % num )
165167

166168
self.window._destroying = False
167-
168-
if matplotlib.rcParams['toolbar'] == 'classic':
169-
print "Classic toolbar is not yet supported"
170-
#self.toolbar = NavigationToolbarQT( centralWidget, canvas )
171-
self.toolbar = None
172-
elif matplotlib.rcParams['toolbar'] == 'toolbar2':
173-
self.toolbar = NavigationToolbar2QT( centralWidget, canvas )
174-
else:
175-
self.toolbar = None
169+
170+
self.toolbar = self._get_toolbar(self.canvas, centralWidget)
176171

177172
# Use a vertical layout for the plot and the toolbar. Set the
178173
# stretch to all be in the plot so the toolbar doesn't resize.
179-
layout = qt.QVBoxLayout( centralWidget )
180-
layout.addWidget( self.canvas, 1 )
174+
self.layout = qt.QVBoxLayout( centralWidget )
175+
self.layout.addWidget( self.canvas, 1 )
176+
181177
if self.toolbar:
182-
layout.addWidget( self.toolbar, 0 )
178+
self.layout.addWidget( self.toolbar, 0 )
183179

184180
self.window.setCentralWidget( centralWidget )
185181

@@ -212,6 +208,21 @@ def _widgetclosed( self ):
212208
def _widgetCloseEvent( self, event ):
213209
self._widgetclosed()
214210
qt.QWidget.closeEvent( self.window, event )
211+
212+
def _get_toolbar(self, canvas, parent):
213+
# must be inited after the window, drawingArea and figure
214+
# attrs are set
215+
if matplotlib.rcParams['toolbar'] == 'classic':
216+
print "Classic toolbar is not yet supported"
217+
elif matplotlib.rcParams['toolbar'] == 'toolbar2':
218+
toolbar = NavigationToolbar2QT(canvas, parent)
219+
else:
220+
toolbar = None
221+
return toolbar
222+
223+
def set_canvas_size(self, width, height):
224+
'set the canvas size in pixels'
225+
self.window.resize(width, height)
215226

216227
def destroy( self, *args ):
217228
if self.window._destroying: return
@@ -230,10 +241,11 @@ class NavigationToolbar2QT( NavigationToolbar2, qt.QWidget ):
230241
('Pan', 'Pan axes with left mouse, zoom with right', 'move.ppm', 'pan'),
231242
('Zoom', 'Zoom to rectangle','zoom_to_rect.ppm', 'zoom'),
232243
(None, None, None, None),
244+
('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
233245
('Save', 'Save the figure','filesave.ppm', 'save_figure'),
234246
)
235247

236-
def __init__( self, parent, canvas ):
248+
def __init__( self, canvas, parent ):
237249
self.canvas = canvas
238250
self.buttons = {}
239251

@@ -282,6 +294,9 @@ def _init_toolbar( self ):
282294
self.locLabel.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Ignored,
283295
qt.QSizePolicy.Ignored))
284296
self.layout.addWidget( self.locLabel, 1 )
297+
298+
# reference holder for subplots_adjust window
299+
self.adj_window = None
285300

286301
def pan( self, *args ):
287302
self.buttons[ 'Zoom' ].setOn( False )
@@ -312,6 +327,32 @@ def draw_rubberband( self, event, x0, y0, x1, y1 ):
312327

313328
rect = [ int(val)for val in min(x0,x1), min(y0, y1), w, h ]
314329
self.canvas.drawRectangle( rect )
330+
331+
def configure_subplots(self):
332+
self.adj_window = qt.QMainWindow()
333+
win = self.adj_window
334+
win.setCaption("Subplot Configuration Tool")
335+
336+
toolfig = Figure(figsize=(6,3))
337+
toolfig.subplots_adjust(top=0.9)
338+
w = int (toolfig.bbox.width())
339+
h = int (toolfig.bbox.height())
340+
341+
canvas = self._get_canvas(toolfig)
342+
tool = SubplotTool(self.canvas.figure, toolfig)
343+
centralWidget = qt.QWidget(win)
344+
canvas.reparent(centralWidget, qt.QPoint(0, 0))
345+
win.setCentralWidget(centralWidget)
346+
347+
layout = qt.QVBoxLayout(centralWidget)
348+
layout.addWidget(canvas, 1)
349+
350+
win.resize(w, h)
351+
canvas.setFocus()
352+
win.show()
353+
354+
def _get_canvas(self, fig):
355+
return FigureCanvasQT(fig)
315356

316357
def save_figure( self ):
317358
fname = qt.QFileDialog.getSaveFileName()

lib/matplotlib/backends/backend_qtagg.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
from __future__ import division
55

66
import os, sys
7+
import matplotlib
78
from matplotlib import verbose
89
from matplotlib.cbook import enumerate
910
from matplotlib.figure import Figure
1011

1112
from backend_agg import FigureCanvasAgg
1213
from backend_qt import qt, FigureManagerQT, FigureCanvasQT,\
13-
show, draw_if_interactive, backend_version
14+
show, draw_if_interactive, backend_version, \
15+
NavigationToolbar2QT
1416

1517
DEBUG = False
1618

@@ -23,7 +25,23 @@ def new_figure_manager( num, *args, **kwargs ):
2325
FigureClass = kwargs.pop('FigureClass', Figure)
2426
thisFig = FigureClass( *args, **kwargs )
2527
canvas = FigureCanvasQTAgg( thisFig )
26-
return FigureManagerQT( canvas, num )
28+
return FigureManagerQTAgg( canvas, num )
29+
30+
class NavigationToolbar2QTAgg(NavigationToolbar2QT):
31+
def _get_canvas(self, fig):
32+
return FigureCanvasQTAgg(fig)
33+
34+
class FigureManagerQTAgg(FigureManagerQT):
35+
def _get_toolbar(self, canvas, parent):
36+
# must be inited after the window, drawingArea and figure
37+
# attrs are set
38+
if matplotlib.rcParams['toolbar']=='classic':
39+
print "Classic toolbar is not yet supported"
40+
elif matplotlib.rcParams['toolbar']=='toolbar2':
41+
toolbar = NavigationToolbar2QTAgg(canvas, parent)
42+
else:
43+
toolbar = None
44+
return toolbar
2745

2846
class FigureCanvasQTAgg( FigureCanvasQT, FigureCanvasAgg ):
2947
"""

0 commit comments

Comments
 (0)