Added MatplotLib widget to Pythoncard - and it works perfectly!!!

John Henry <[email protected]> Tue, 12 Apr 2011 23:29:00 -0700 (PDT)
Newsgroups gmane.comp.python.pythoncard
Message-ID <[email protected]>
--===============8790807501556207150==
Content-Type: multipart/alternative; boundary="0-1440440670-1302676140=:91318"

--0-1440440670-1302676140=:91318
Content-Type: text/plain; charset=us-ascii

Glad to announce that I got it working!!!
 --
John Henry


>
>From: John Henry <[email protected]>
>To: [email protected]
>Sent: Tue, April 12, 2011 12:59:22 PM
>Subject: Fw: Adding MatplotLib widget to Pythoncard - and ALMOST works 
perfectly
>
>
>Oops.  The image was too big.  Reattach.
> --
>John Henry
>
>
>
>>
>>----- Forwarded Message ----
>>From: John Henry <[email protected]>
>>To: [email protected]
>>Sent: Tue, April 12, 2011 12:46:40 PM
>>Subject: Adding MatplotLib widget to  Pythoncard - and ALMOST works perfectly
>>
>>
>>I'm *this* close to a working Matplotlib widget in Pythoncard (see screen shot 
>>attached).  Everything works except somehow all of my mouse events are gone, 
>>plus the canvas is not getting the the size and paint events.  I wish somebody 
>>more knowledgeable in Pythoncard can help (via private email if you like).
>>
>>
>>From the user perspective, the code looks like below (with some non-essential 
>>details omitted) - look how simple it is (compare with the original code 
>>at http://eli.thegreenplace.net/files/prog_code/wx_mpl_bars.py.txt
>>
>>
>>
>>
>>##############
>>"""
>>This demo demonstrates how to embed a matplotlib (mpl) plot 
>>into a Pythoncard GUI application,  including:
>>
>>
>>* Using the navigation  toolbar
>>* Adding data to the plot
>>* Dynamically modifying the plot's properties
>>* Processing mpl events
>>* Saving the plot to a file from a menu
>>
>>
>>The main goal is to serve as a basis for developing rich Pythoncard GUI
>>applications featuring mpl plots (using the mpl OO API).
>>
>>
>>Eli Bendersky ([email protected])
>>License: this code is in the public domain
>>Last modified: 30.07.2008
>>"""
>>import os
>>import pprint
>>import random
>>import wx
>>
>>
>># The recommended way to use wx with mpl is with the WXAgg
>># backend. 
>>#
>>import matplotlib
>>matplotlib.use('WXAgg')
>>from matplotlib.figure import Figure
>>from matplotlib.backends.backend_wxagg import \
>>    NavigationToolbar2WxAgg as  NavigationToolbar
>>
>>
>>from PythonCard import model, dialog
>>
>>
>>from pc_mpl_bars_rsrc import rsrc as BarsFrame_rsrc
>>
>>
>>class BarsFrame(model.MDIParentBackground):
>>def __init__(self, parent, rsrc):
>># Create the mpl Figure and FigCanvas objects. 
>># 5x4 inches, 100 dots-per-inch
>>#
>>model.MDIParentBackground.__init__(self, parent, aBgRsrc=rsrc)
>>def on_initialize(self, event):
>>self.components.slider_width.SetTickFreq(10, 1)
>>
>>
>># Since we have only one plot, we can use add_axes 
>># instead of add_subplot, but then the subplot
>># configuration tool in the navigation toolbar wouldn't
>># work.
>>#
>>figure=self.components.canvas.fig
>>self.axes = figure.add_subplot(111)
>># Create the navigation toolbar, tied to the canvas
>>#
>>self.toolbar = NavigationToolbar(self.components.canvas)
>>self.sizer(event)
>>self.data = [5, 6, 9, 14]
>>self.components.textbox.text=(' '.join(map(str,  self.data)))
>>
>>
>>self.draw_figure()
>>
>>
>>def sizer(self, event):
>><...sizer code skipped ...>
>>
>>
>>def draw_figure(self):
>>""" Redraws the figure
>>"""
>>str = self.components.textbox.text
>>self.data = map(int, str.split())
>>x = range(len(self.data))
>>
>>
>># clear the axes and redraw the plot anew
>>#
>>self.axes.clear()
>>self.axes.grid(self.components.cb_grid.checked)
>>self.axes.bar(
>>left=x, 
>>height=self.data, 
>>width=self.components.slider_width.GetValue() / 100.0, 
>>align='center', 
>>alpha=0.44,
>>picker=5)
>>self.components.canvas.draw()
>>def on_exit(self, event):
>>self.Destroy()
>>
>>
>>if __name__ == '__main__':
>>app =  model.Application(BarsFrame,  rsrc=BarsFrame_rsrc)
>>app.MainLoop()
>>
>> --
>>John Henry
--0-1440440670-1302676140=:91318
Content-Type: text/html; charset=us-ascii

<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:'times new roman', 'new york', times, serif;font-size:12pt"><div></div><div>Glad to announce that I got it working!!!<br>&nbsp;</div>--<br>John Henry<div><br></div><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><br><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><font size="2" face="Tahoma"><b><span style="font-weight: bold;">From:</span></b> John Henry &lt;[email protected]&gt;<br><b><span style="font-weight: bold;">To:</span></b> [email protected]<br><b><span style="font-weight: bold;">Sent:</span></b> 
 Tue, April 12, 2011 12:59:22 PM<br><b><span style="font-weight: bold;">Subject:</span></b> Fw: Adding MatplotLib widget to Pythoncard - and ALMOST works
 perfectly<br></font><br>
<meta http-equiv="x-dns-prefetch-control" content="off"><div style="font-family:times, serif;font-size:12pt;"><div></div><div>Oops. &nbsp;The image was too big. &nbsp;Reattach.<br>&nbsp;</div>--<br>John Henry<div><br></div><blockquote style="border-left:2px solid rgb(16, 16, 255);margin-left:5px;padding-left:5px;"><div style="font-family:times new roman, new york, times, serif;font-size:12pt;"><br><div style="font-family:times new roman, new york, times, serif;font-size:12pt;"><font size="2" face="Tahoma">----- Forwarded Message ----<br><b><span style="font-weight:bold;">From:</span></b> John Henry &lt;[email protected]&gt;<br><b><span style="font-weight:bold;">To:</span></b> [email protected]<br><b><span style="font-weight:bold;">Sent:</span></b> Tue, April 12, 2011 1
 2:46:40 PM<br><b><span style="font-weight:bold;">Subject:</span></b> Adding MatplotLib widget to
 Pythoncard - and ALMOST works perfectly<br></font><br>
<div style="font-family:times, serif;font-size:12pt;"><div style="color:black;font-family:times, serif;font-size:12pt;"></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="3">I'm *this* close to a working Matplotlib widget in Pythoncard (see screen shot attached). &nbsp;Everything works except somehow all of my mouse events are gone, plus the canvas is not getting the&nbsp;the size and paint events. &nbsp;I wish somebody more&nbsp;</font><font class="Apple-style-span" face="'times new roman', 'new york', times, serif">knowledgeable&nbsp;</font><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="3">in Pythoncard can help (via private email if you like).</font></div><div><font class="Apple-style-span" face="'t
 imes new roman',
 'new york', times, serif"><br></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="3"></font><font class="Apple-style-span" face="'times new roman', 'new york', times, serif">From the user perspective, the code looks like below (with some non-essential details omitted) - look how simple it is (compare with the original code at&nbsp;</font><span><span><a target="_blank" href="http://eli.thegreenplace.net/files/prog_code/wx_mpl_bars.py.txt">http://eli.thegreenplace.net/files/prog_code/wx_mpl_bars.py.txt</a></span></span></div><div><br></div><div><br></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif">##############</font></div><div><font class="Apple-style-span" face="'times new roman', 'new york',
  times, serif"><div>"""</div><div>This demo demonstrates how to embed a matplotlib (mpl) plot&nbsp;</div><div>into a Pythoncard GUI application,
 including:</div><div><br></div><div>* Using the navigation
 toolbar</div><div>* Adding data to the plot</div><div>* Dynamically modifying the plot's properties</div><div>* Processing mpl events</div><div>* Saving the plot to a file from a menu</div><div><br></div><div>The main goal is to serve as a basis for developing rich Pythoncard GUI</div><div>applications featuring mpl plots (using the mpl OO API).</div><div><br></div><div>Eli Bendersky ([email protected])</div><div>License: this code is in the public domain</div><div>Last modified: 30.07.2008</div><div>"""</div><div>import os</div><div>import pprint</div><div>import random</div><div>import wx</div><div><br></div><div># The recommended way to use wx with mpl is with the WXAgg</div><div># backend.&nbsp;</div><div>#</div><div>import matplotlib</div><div>matplotlib.use('WXAgg')</div><div>from ma
 tplotlib.figure import Figure</div><div>from matplotlib.backends.backend_wxagg import \</div><div>&nbsp;&nbsp; &nbsp;NavigationToolbar2WxAgg as
 NavigationToolbar</div><div><br></div><div>from PythonCard import model, dialog</div><div><br></div><div>from pc_mpl_bars_rsrc import rsrc as BarsFrame_rsrc</div><div><br></div><div>class BarsFrame(model.MDIParentBackground):</div><div><span class="Apple-tab-span" style="white-space:pre;">	</span></div><div><span class="Apple-tab-span" style="white-space:pre;">	</span>def __init__(self, parent, rsrc):</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span># Create the mpl Figure and FigCanvas objects.&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span># 5x4 inches, 100 dots-per-inch</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>#</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>model.MDIParent
 Background.__init__(self, parent, aBgRsrc=rsrc)</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span></div><div><span class="Apple-tab-span"
 style="white-space:pre;">	</span>def on_initialize(self, event):</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.components.slider_width.SetTickFreq(10, 1)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span># Since we have only one plot, we can use add_axes&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span># instead of add_subplot, but then the subplot</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span># configuration tool in the navigation toolbar wouldn't</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span># work.</div><div><span class="Apple-tab-span" style="white-space:pre;">
 		</span>#</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>figure=self.components.canvas.fig</div><div><span class="Apple-tab-span"
 style="white-space:pre;">		</span>self.axes = figure.add_subplot(111)</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>#&nbsp;Create the navigation toolbar, tied to the canvas</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>#</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.toolbar = NavigationToolbar(self.components.canvas)</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.sizer(event)</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.dat
 a = [5, 6, 9, 14]</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.components.textbox.text=(' '.join(map(str,
 self.data)))</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.draw_figure()</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre;">	</span>def sizer(self, event):</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>&lt;...sizer code skipped ...&gt;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre;">	</span>def draw_figure(self):</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>""" Redraws the figure</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>"""</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>str = self.components.textbox.text</div><div><span class="Apple-tab-span" style="white-space:pre;">		</sp
 an>self.data = map(int, str.split())</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>x = range(len(self.data))</div><div><br></div><div><span
 class="Apple-tab-span" style="white-space:pre;">		</span># clear the axes and redraw the plot anew</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>#</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.axes.clear()<span class="Apple-tab-span" style="white-space:pre;">		</span></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.axes.grid(self.components.cb_grid.checked)</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.axes.bar(</div><div><span class="Apple-tab-span" style="white-space:pre;">			</span>left=x,&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre;">			</span>height=self.data,&nbsp;</d
 iv><div><span class="Apple-tab-span" style="white-space:pre;">			</span>width=self.components.slider_width.GetValue() / 100.0,&nbsp;</div><div><span
 class="Apple-tab-span" style="white-space:pre;">			</span>align='center',&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre;">			</span>alpha=0.44,</div><div><span class="Apple-tab-span" style="white-space:pre;">			</span>picker=5)</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span></div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.components.canvas.draw()</div><div><span class="Apple-tab-span" style="white-space:pre;">	</span></div><div><span class="Apple-tab-span" style="white-space:pre;">	</span></div><div><span class="Apple-tab-span" style="white-space:pre;">	</span>def on_exit(self, event):</div><div><span class="Apple-tab-span" style="white-space:pre;">		</span>self.Destroy()</div><div><span class="Apple-tab-span" s
 tyle="white-space:pre;">		</span></div><div><br></div><div>if __name__ == '__main__':</div><div><span class="Apple-tab-span" style="white-space:pre;">	</span>app =
 model.Application(BarsFrame,
 rsrc=BarsFrame_rsrc)</div><div><span class="Apple-tab-span" style="white-space:pre;">	</span>app.MainLoop()</div><div><br></div></font><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="3">&nbsp;</font></div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="3">--</font><br><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="3">John Henry</font><div style="color:black;font-family:times, serif;font-size:12pt;"></div>


</div></div></div></blockquote><div style=""></div>


</div><meta http-equiv="x-dns-prefetch-control" content="on"></div></div></blockquote><div style="position:fixed"></div>


</div></body></html>
--0-1440440670-1302676140=:91318--


--===============8790807501556207150==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
Forrester Wave Report - Recovery time is now measured in hours and minutes
not days. Key insights are discussed in the 2010 Forrester Wave Report as
part of an in-depth evaluation of disaster recovery service providers.
Forrester found the best-in-class provider in terms of services and vision.
Read this report now!  http://p.sf.net/sfu/ibm-webcastpromo
--===============8790807501556207150==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Pythoncard-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pythoncard-users

--===============8790807501556207150==--