-
Notifications
You must be signed in to change notification settings - Fork 0
/
barplot_wx.py
213 lines (150 loc) · 6.1 KB
/
barplot_wx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
###########################################################################
import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib import animation
import wx
import time
import random
#import wx.xrc
###########################################################################
## Class Testu_logs
###########################################################################
fps = 0.
drawing = False
start_time = time.time()
class Testu_logs ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Testu logs", pos = wx.DefaultPosition, size = wx.Size( 700,450 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.HORIZONTAL )
self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer1.Add( self.m_panel1, 1, wx.EXPAND, 5 )
self.m_panel2 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.BORDER_SUNKEN|wx.TAB_TRAVERSAL )
self.m_panel2.SetMinSize( wx.Size( 150,-1 ) )
self.figure = plt.Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.canvas,1,wx.EXPAND)
self.SetSizer(self.sizer)
global Blit
Blit = True
##########TIMER
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.draw, self.timer)
###############
bSizer2 = wx.BoxSizer( wx.VERTICAL )
bSizer2.Add( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_button1 = wx.Button( self.m_panel2, wx.ID_ANY, u"Variants A", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer2.Add( self.m_button1, 0, wx.ALL, 5 )
self.m_button2 = wx.Button( self.m_panel2, wx.ID_ANY, u"Variants B", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer2.Add( self.m_button2, 0, wx.ALL, 5 )
self.toggleBtn = wx.Button(self.m_panel2, wx.ID_ANY, "Start")
bSizer2.Add( self.toggleBtn, 0, wx.ALL, 5 )
bSizer2.Add( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_panel2.SetSizer( bSizer2 )
self.m_panel2.Layout()
bSizer2.Fit( self.m_panel2 )
bSizer1.Add( self.m_panel2, 0, wx.EXPAND, 5 )
self.SetSizer( bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
self.m_statusBar1 = self.CreateStatusBar( 1, wx.STB_SIZEGRIP|wx.BORDER_RAISED, wx.ID_ANY )
# Connect Events
self.Bind( wx.EVT_CLOSE, self.OnClose )
self.Bind( wx.EVT_IDLE, self.OnIdle )
self.m_button1.Bind( wx.EVT_BUTTON, self.OnVariantsA )
self.m_button2.Bind( wx.EVT_BUTTON, self.OnVariantsB )
self.toggleBtn.Bind(wx.EVT_BUTTON, self.onToggle)
########################
x=range(1,6)
y=50
col=[]
global barcollection
self.barcollection = self.axes.bar(x,y, color=col)
self.animator = animation.FuncAnimation(self.figure,self.animate,blit = False, interval=50)
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def OnClose( self, event ):
#self.Close()
#pass
event.Skip()
def OnIdle( self, event ):
event.Skip()
def onToggle(self, event):
btnLabel = self.toggleBtn.GetLabel()
if btnLabel == "Start":
print ("starting timer...")
self.timer.Start(1)
self.toggleBtn.SetLabel("Stop")
else:
print ("timer stopped!")
self.timer.Stop()
self.toggleBtn.SetLabel("Start")
def OnVariantsA( self, event ):
global Blit
Blit = True
print ("With Blit")
def OnVariantsB( self, event ):
global Blit
print ("Without Blit")
Blit = False
#def TimeInterval(self, event):
def animate(self, i):
global drawing
if drawing:
return
drawing = True
if Blit == True:
for i in range(len(self.barcollection)):
val = random.randint(0,50)
self.barcollection[i].set_height(val)
#print(i)
if val < 10:
#print("blue")
self.barcollection[i].set_color("blue")
elif val >= 25:
#print("green")
self.barcollection[i].set_color("green")
else:
#print("red")
self.barcollection[i].set_color("red")
drawing = False
def draw(self, event):
#print("start drawing")
global start_time
global k, fps
global Blit
if Blit == True:
pass
else:
plt.clf()
x=range(1,6)
for i in range(len(x)):
y=random.randint(0,50)
self.barcollection = self.axes.bar(i,y)
#self.figure.canvas.flush_events()
#####
current_time = time.time()
fps = int( ( 9 * fps + 1.0 / ( current_time - start_time ) ) / 10 )
#fps = str( int( fps ) )
self.m_statusBar1.SetStatusText( 'FPS:{0:3d}'.format( fps ) )
start_time = current_time
class MainApp(wx.App):
def OnInit(self):
mainFrame = Testu_logs(None)
mainFrame.Show(True)
return True
if __name__ =='__main__':
# start time of the loop
app = MainApp()
app.MainLoop()
#app = wx.App( False )
#frame = Testu_logs( None )
#frame.Show( True )
#start the applications
#app.MainLoop()