Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatplotlib_example.py
More file actions
Latest commit
executable file
·109 lines (87 loc) · 2.85 KB
/
Copy pathmatplotlib_example.py
File metadata and controls
executable file
·109 lines (87 loc) · 2.85 KB
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
importmatplotlib
matplotlib.use("Agg")
importmatplotlib.backends.backend_aggasagg
importpygameaspg
importpylab
importnumpyasnp, numpy.randomasrnd
frommathimport*
fromui_exampleimport*
fromsnd_exampleimport*
defmake_fig(fig,view):
#Interface with matplotlib, draw plot on a given UI_Item "view"
canvas=agg.FigureCanvasAgg(fig)
canvas.draw()
renderer=canvas.get_renderer()
raw_data=renderer.tostring_rgb()
size=canvas.get_width_height()
#Create pygame surface from string
surf=pg.image.fromstring(raw_data, size, "RGB")
#Set that surface as the default image of view
view.images['idle']=surf
view.update_sprite()
pylab.close(fig)
defhist_example(view,nb=100,ui=None):
'''Draw a histogram of random numbers between 0 and 1'''
try:
nb=nb()
except:
pass
fig=pylab.figure(figsize=[4, 4], # Inches
dpi=100, # 100 dots per inch, so the resulting buffer is 400x400 pixels
)
ax=fig.gca()
ax.hist([rnd.random() forxinrange(nb)])
make_fig(fig,view)
defplot_example(view,puls=1,ui=None):
'''Draw a histogram of random numbers between 0 and 1'''
try:
puls=puls()
except:
pass
fig=pylab.figure(figsize=[4, 4], # Inches
dpi=100, # 100 dots per inch, so the resulting buffer is 400x400 pixels
)
ax=fig.gca()
ax.plot([2*pi*x/100.forxinrange(100)],[sin(puls*2*pi*x/100.) forxinrange(100)])
make_fig(fig,view)
defplay_audio(view,harmonic=1,**kwargs):
#Play a sound that is an harmonic of A3 (220Hz)
try:
harmonic=harmonic()
except:
pass
snd=make_sound(2.,harmonic*220)
snd.play()
defuinit(screen):
ui=UI(screen)
#Counter: Harmonic
view=ui.add('view',pos=(10,80),size=(100,20),text='Harmonic:')
counter=ui.add('counter',pos=(10,100),size=(40,40))
counter.set_val(1)
#Display element for the plot
view=ui.add('view',pos=(200,0),size=(400,400))
view.bind(view.clean)
#When the counter changes, call plot_example on the view
counter.bind(plot_example,view,counter.get_val)
plot_example(view,counter.get_val) #Do it with the initial value
#Buttons for incrementing counter
plus=ui.add('button',pos=(10,150),text='+')
plus.bind(counter.increment,1)
minus=ui.add('button',pos=(10,200),text='-')
minus.bind(counter.increment,-1)
#Play button
playbutton=ui.add('button',pos=(10,10),text='Play')
playbutton.bind(play_audio,view,counter.get_val)
returnui
defmain():
pg.init()
screen=pg.display.set_mode((600, 400), pg.DOUBLEBUF)
ui=uinit(screen)
alive=True
whilealive:
draw(ui)
alive=logic(ui)
pg.display.quit()
pg.quit()
if__name__in'__main__':
main()