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

Skip to content

Commit cd2a12f

Browse files
committed
added embedding in wx4
svn path=/trunk/matplotlib/; revision=399
1 parent 78d8122 commit cd2a12f

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

examples/embedding_in_wx4.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env python
2+
"""
3+
An example of how to use wx or wxagg in an application with a custom
4+
toolbar
5+
"""
6+
7+
from matplotlib.numerix import arange, sin, pi
8+
9+
import matplotlib
10+
11+
# uncomment the following to use wx rather than wxagg
12+
#matplotlib.use('WX')
13+
#from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
14+
15+
# comment out the following to use wx rather than wxagg
16+
matplotlib.use('WXAgg')
17+
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
18+
from matplotlib.backends.backend_wx import NavigationToolbarWx as NavigationToolbar
19+
20+
from matplotlib.backends.backend_wx import _load_bitmap
21+
from matplotlib.figure import Figure
22+
from matplotlib.numerix import rand
23+
24+
from wxPython.wx import *
25+
26+
class MyNavigationToolbar(NavigationToolbar):
27+
"""
28+
Extend the default wx toolbar with your own event handlers
29+
"""
30+
ON_CUSTOM = wxNewId()
31+
def __init__(self, canvas, cankill):
32+
NavigationToolbar.__init__(self, canvas, cankill)
33+
34+
# for simplicity I'm going to reuse a bitmap from wx, you'll
35+
# probably want to add your own.
36+
self.AddSimpleTool(self.ON_CUSTOM, _load_bitmap('stock_left.xpm'),
37+
'Click me', 'Activate custom contol')
38+
EVT_TOOL(self, self.ON_CUSTOM, self._on_custom)
39+
40+
def _on_custom(self, evt):
41+
# add some text to the axes in a random location in axes (0,1)
42+
# coords) with a random color
43+
44+
# get the axes
45+
ax = self.canvas.figure.axes[0]
46+
47+
# generate a random location can color
48+
x,y = tuple(rand(2))
49+
rgb = tuple(rand(3))
50+
51+
# add the text and draw
52+
ax.text(x, y, 'You clicked me',
53+
transform=ax.transAxes,
54+
color=rgb)
55+
self.canvas.draw()
56+
evt.Skip()
57+
58+
59+
class CanvasFrame(wxFrame):
60+
61+
def __init__(self):
62+
wxFrame.__init__(self,None,-1,
63+
'CanvasFrame',size=(550,350))
64+
65+
self.SetBackgroundColour(wxNamedColor("WHITE"))
66+
67+
self.figure = Figure(figsize=(5,4), dpi=100)
68+
self.axes = self.figure.add_subplot(111)
69+
t = arange(0.0,3.0,0.01)
70+
s = sin(2*pi*t)
71+
72+
self.axes.plot(t,s)
73+
74+
self.canvas = FigureCanvas(self, -1, self.figure)
75+
76+
self.sizer = wxBoxSizer(wxVERTICAL)
77+
self.sizer.Add(self.canvas, 1, wxTOP | wxLEFT | wxEXPAND)
78+
# Capture the paint message
79+
EVT_PAINT(self, self.OnPaint)
80+
81+
self.toolbar = MyNavigationToolbar(self.canvas, True)
82+
self.toolbar.Realize()
83+
if wxPlatform == '__WXMAC__':
84+
# Mac platform (OSX 10.3, MacPython) does not seem to cope with
85+
# having a toolbar in a sizer. This work-around gets the buttons
86+
# back, but at the expense of having the toolbar at the top
87+
self.SetToolBar(self.toolbar)
88+
else:
89+
# On Windows platform, default window size is incorrect, so set
90+
# toolbar width to figure width.
91+
tw, th = self.toolbar.GetSizeTuple()
92+
fw, fh = self.canvas.GetSizeTuple()
93+
# By adding toolbar in sizer, we are able to put it at the bottom
94+
# of the frame - so appearance is closer to GTK version.
95+
# As noted above, doesn't work for Mac.
96+
self.toolbar.SetSize(wxSize(fw, th))
97+
self.sizer.Add(self.toolbar, 0, wxLEFT | wxEXPAND)
98+
99+
# update the axes menu on the toolbar
100+
self.toolbar.update()
101+
self.SetSizer(self.sizer)
102+
self.Fit()
103+
104+
105+
def OnPaint(self, event):
106+
self.canvas.draw()
107+
108+
class App(wxApp):
109+
110+
def OnInit(self):
111+
'Create the main window and insert the custom frame'
112+
frame = CanvasFrame()
113+
frame.Show(true)
114+
115+
return true
116+
117+
app = App(0)
118+
app.MainLoop()

0 commit comments

Comments
 (0)