matplotlib.figure
import
class
MyNavigationToolbar
(
NavigationToolbar
):
"""Extend the default wx toolbar with your own event handlers."""
def
__init__
(
self
,
canvas
):
super
()
.
__init__
(
canvas
)
bmp
=
(
wx
.
ART_CROSS_MARK
,
wx
.
ART_TOOLBAR
)
tool
=
self
.
AddTool
(
wx
.
ID_ANY
,
'Click me'
,
bmp
,
'Activate custom control'
)
self
.
Bind
(
wx
.
EVT_TOOL
,
self
.
_on_custom
,
id
=
tool
.
GetId
())
def
_on_custom
(
self
,
event
):
ax
=
self
.
canvas
.
figure
.
axes
[
0
]
x
,
y
=
(
2
)
rgb
=
(
3
)
ax
.
text
(
x
,
y
,
'You clicked me'
,
transform
=
ax
.
transAxes
,
color
=
rgb
)
self
.
canvas
.
draw
()
event
.
Skip
()
class
CanvasFrame
(
):
def
__init__
(
self
):
super
()
.
__init__
(
None
,
-
1
,
'CanvasFrame'
,
size
=
(
550
,
350
))
self
.
figure
=
(
figsize
=
(
5
,
4
),
dpi
=
100
)
self
.
axes
=
self
.
figure
.
add_subplot
()
t
=
(
0.0
,
3.0
,
0.01
)
s
=
(
2
*
*
t
)
self
.
axes
.
plot
(
t
,
s
)
self
.
canvas
=
FigureCanvas
(
self
,
-
1
,
self
.
figure
)
self
.
sizer
=
(
wx
.
VERTICAL
)
self
.
sizer
.
Add
(
self
.
canvas
,
1
,
wx
.
TOP
|
wx
.
LEFT
|
wx
.
EXPAND
)
self
.
toolbar
=
MyNavigationToolbar
(
self
.
canvas
)
self
.
toolbar
.
Realize
()
self
.
sizer
.
Add
(
self
.
toolbar
,
0
,
wx
.
LEFT
|
wx
.
EXPAND
)
self
.
toolbar
.
update
()
self
.
SetSizer
(
self
.
sizer
)
self
.
Fit
()
class
App
(
):
def
OnInit
(
self
):
"""Create the main window and insert the custom frame."""
frame
=
CanvasFrame
()
frame
.
Show
(
True
)
return
True
if
__name__
==
"__main__"
:
app
=
App
()
app
.
MainLoop
()