Skip to content

Commit b718a91

Browse files
committed
axes was not calling artist set_axes method in set artist props
svn path=/trunk/matplotlib/; revision=5707
1 parent db884dd commit b718a91

File tree

6 files changed

+134
-56
lines changed

6 files changed

+134
-56
lines changed

examples/widgets/menu.py

+89-49
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,75 @@
11
import numpy as np
22
import matplotlib
3+
import matplotlib.colors as colors
34
import matplotlib.patches as patches
45
import matplotlib.mathtext as mathtext
56
import matplotlib.pyplot as plt
67
import matplotlib.artist as artist
78
import matplotlib.image as image
89

9-
matplotlib.rc('image', origin='upper')
10+
11+
class ItemProperties:
12+
def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow', alpha=1.0):
13+
self.fontsize = fontsize
14+
self.labelcolor = labelcolor
15+
self.bgcolor = bgcolor
16+
self.alpha = alpha
17+
18+
self.labelcolor_rgb = colors.colorConverter.to_rgb(labelcolor)
19+
self.bgcolor_rgb = colors.colorConverter.to_rgb(bgcolor)
1020

1121
class MenuItem(artist.Artist):
1222
parser = mathtext.MathTextParser("Bitmap")
1323
padx = 5
1424
pady =5
15-
def __init__(self, fig, labelstr):
25+
def __init__(self, fig, labelstr, props=None, hoverprops=None, on_select=None):
1626
artist.Artist.__init__(self)
27+
1728
self.set_figure(fig)
29+
self.labelstr = labelstr
30+
31+
if props is None:
32+
props = ItemProperties()
33+
34+
if hoverprops is None:
35+
hoverprops = ItemProperties()
36+
37+
self.props = props
38+
self.hoverprops = hoverprops
39+
1840

41+
self.on_select = on_select
1942

20-
x, self.depth = self.parser.to_rgba(
21-
labelstr, color='black', fontsize=14, dpi=100)
22-
xHover, depth = self.parser.to_rgba(
23-
labelstr, color='white', fontsize=14, dpi=100)
43+
x, self.depth = self.parser.to_mask(
44+
labelstr, fontsize=props.fontsize, dpi=fig.dpi)
45+
46+
if props.fontsize!=hoverprops.fontsize:
47+
raise NotImplementedError('support for different font sizes not implemented')
2448

2549

2650
self.labelwidth = x.shape[1]
2751
self.labelheight = x.shape[0]
28-
print 'h', self.labelheight
29-
self.label = image.FigureImage(fig)
30-
self.label.set_array(x.astype(float)/255.)
31-
32-
self.labelHover = image.FigureImage(fig)
33-
self.labelHover.set_array(xHover.astype(float)/255.)
3452

53+
self.labelArray = np.zeros((x.shape[0], x.shape[1], 4))
54+
self.labelArray[:,:,-1] = x/255.
3555

56+
self.label = image.FigureImage(fig, origin='upper')
57+
self.label.set_array(self.labelArray)
3658

3759
# we'll update these later
38-
self.rect = patches.Rectangle((0,0), 1,1, facecolor='yellow', alpha=0.2)
39-
self.rectHover = patches.Rectangle((0,0), 1,1, facecolor='blue', alpha=0.2)
60+
self.rect = patches.Rectangle((0,0), 1,1)
61+
62+
self.set_hover_props(False)
63+
64+
fig.canvas.mpl_connect('button_release_event', self.check_select)
4065

66+
def check_select(self, event):
67+
over, junk = self.rect.contains(event)
68+
if not over:
69+
return
4170

71+
if self.on_select is not None:
72+
self.on_select(self)
4273

4374
def set_extent(self, x, y, w, h):
4475
print x, y, w, h
@@ -47,65 +78,64 @@ def set_extent(self, x, y, w, h):
4778
self.rect.set_width(w)
4879
self.rect.set_height(h)
4980

50-
self.rectHover.set_x(x)
51-
self.rectHover.set_y(y)
52-
self.rectHover.set_width(w)
53-
self.rectHover.set_height(h)
54-
5581
self.label.ox = x+self.padx
5682
self.label.oy = y-self.depth+self.pady/2.
5783

5884
self.rect._update_patch_transform()
59-
self.rectHover._update_patch_transform()
60-
self.labelHover.ox = x+self.padx
61-
self.labelHover.oy = y-self.depth+self.pady/2.
6285
self.hover = False
6386

64-
self.activeRect = self.rect
65-
self.activeLabel = self.label
66-
6787
def draw(self, renderer):
68-
self.activeRect.draw(renderer)
69-
self.activeLabel.draw(renderer)
88+
self.rect.draw(renderer)
89+
self.label.draw(renderer)
90+
91+
def set_hover_props(self, b):
92+
if b:
93+
props = self.hoverprops
94+
else:
95+
props = self.props
96+
97+
r, g, b = props.labelcolor_rgb
98+
self.labelArray[:,:,0] = r
99+
self.labelArray[:,:,1] = g
100+
self.labelArray[:,:,2] = b
101+
self.label.set_array(self.labelArray)
102+
self.rect.set(facecolor=props.bgcolor, alpha=props.alpha)
70103

71104
def set_hover(self, event):
72105
'check the hover status of event and return true if status is changed'
73106
b,junk = self.rect.contains(event)
74-
if b:
75-
self.activeRect = self.rectHover
76-
self.activeLabel = self.labelHover
77-
else:
78-
self.activeRect = self.rect
79-
self.activeLabel = self.label
80107

81-
h = self.hover
108+
changed = (b != self.hover)
109+
110+
if changed:
111+
self.set_hover_props(b)
112+
113+
82114
self.hover = b
83-
return b!=h
115+
return changed
84116

85117
class Menu:
86118

87-
def __init__(self, fig, labels):
119+
def __init__(self, fig, menuitems):
88120
self.figure = fig
89121
fig.suppressComposite = True
90-
menuitems = []
91-
self.numitems = len(labels)
92-
for label in labels:
93-
menuitems.append(MenuItem(fig, label))
94122

95123
self.menuitems = menuitems
96-
124+
self.numitems = len(menuitems)
97125

98126
maxw = max([item.labelwidth for item in menuitems])
99127
maxh = max([item.labelheight for item in menuitems])
100128

101129

102130
totalh = self.numitems*maxh + (self.numitems+1)*2*MenuItem.pady
103131

132+
104133
x0 = 100
105134
y0 = 400
106135

107136
width = maxw + 2*MenuItem.padx
108137
height = maxh+MenuItem.pady
138+
109139
for item in menuitems:
110140
left = x0
111141
bottom = y0-maxh-MenuItem.pady
@@ -122,17 +152,27 @@ def __init__(self, fig, labels):
122152
def on_move(self, event):
123153
draw = False
124154
for item in self.menuitems:
125-
b = item.set_hover(event)
126-
draw = b
127-
128-
if draw:
129-
print 'draw'
130-
self.figure.canvas.draw()
155+
draw = item.set_hover(event)
156+
if draw:
157+
self.figure.canvas.draw()
158+
break
131159

132160

133161
fig = plt.figure()
134-
menu = Menu(fig, ('open', 'close', 'save', 'save as', 'quit'))
135-
162+
fig.subplots_adjust(left=0.3)
163+
props = ItemProperties(labelcolor='black', bgcolor='yellow',
164+
fontsize=15, alpha=0.2)
165+
hoverprops = ItemProperties(labelcolor='white', bgcolor='blue',
166+
fontsize=15, alpha=0.2)
167+
168+
menuitems = []
169+
for label in ('open', 'close', 'save', 'save as', 'quit'):
170+
def on_select(item):
171+
print 'you selected', item.labelstr
172+
item = MenuItem(fig, label, props=props, hoverprops=hoverprops, on_select=on_select)
173+
menuitems.append(item)
174+
175+
menu = Menu(fig, menuitems)
136176
plt.show()
137177

138178

lib/matplotlib/axes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ def _set_artist_props(self, a):
813813
a.set_figure(self.figure)
814814
if not a.is_transform_set():
815815
a.set_transform(self.transData)
816-
a.axes = self
816+
817+
a.set_axes(self)
817818

818819
def _gen_axes_patch(self):
819820
"""

lib/matplotlib/pyplot.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ class that will be passed on to :meth:`new_figure_manager` in the
202202
frameon=frameon,
203203
FigureClass=FigureClass,
204204
**kwargs)
205+
206+
# make this figure current on button press event
207+
def make_active(event):
208+
_pylab_helpers.Gcf.set_active(figManager)
209+
210+
cid = figManager.canvas.mpl_connect('button_press_event', make_active)
211+
figManager._cidgcf = cid
212+
205213
_pylab_helpers.Gcf.set_active(figManager)
206214
figManager.canvas.figure.number = num
207215

@@ -252,17 +260,21 @@ def close(*args):
252260
if len(args)==0:
253261
figManager = _pylab_helpers.Gcf.get_active()
254262
if figManager is None: return
255-
else: _pylab_helpers.Gcf.destroy(figManager.num)
263+
else:
264+
figManager.canvas.mpl_disconnect(figManager._cidgcf)
265+
_pylab_helpers.Gcf.destroy(figManager.num)
256266
elif len(args)==1:
257267
arg = args[0]
258268
if arg=='all':
259269
for manager in _pylab_helpers.Gcf.get_all_fig_managers():
270+
manager.canvas.mpl_disconnect(manager._cidgcf)
260271
_pylab_helpers.Gcf.destroy(manager.num)
261272
elif isinstance(arg, int):
262273
_pylab_helpers.Gcf.destroy(arg)
263274
elif isinstance(arg, Figure):
264275
for manager in _pylab_helpers.Gcf.get_all_fig_managers():
265276
if manager.canvas.figure==arg:
277+
manager.canvas.mpl_disconnect(manager._cidgcf)
266278
_pylab_helpers.Gcf.destroy(manager.num)
267279
else:
268280
raise TypeError('Unrecognized argument type %s to close'%type(arg))

src/_backend_agg.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ Py::Object BufferRegion::to_string(const Py::Tuple &args) {
9090
return Py::String(PyString_FromStringAndSize((const char*)data, height*stride), true);
9191
}
9292

93+
Py::Object BufferRegion::set_x(const Py::Tuple &args) {
94+
args.verify_length(1);
95+
size_t x = Py::Int( args[0] );
96+
rect.x1 = x;
97+
return Py::Object();
98+
}
99+
100+
Py::Object BufferRegion::set_y(const Py::Tuple &args) {
101+
args.verify_length(1);
102+
size_t y = Py::Int( args[0] );
103+
rect.y1 = y;
104+
return Py::Object();
105+
}
106+
93107
Py::Object BufferRegion::to_string_argb(const Py::Tuple &args) {
94108
// owned=true to prevent memory leak
95109
Py_ssize_t length;
@@ -1608,6 +1622,13 @@ void BufferRegion::init_type() {
16081622
behaviors().name("BufferRegion");
16091623
behaviors().doc("A wrapper to pass agg buffer objects to and from the python level");
16101624

1625+
1626+
add_varargs_method("set_x", &BufferRegion::set_x,
1627+
"set_x(x)");
1628+
1629+
add_varargs_method("set_y", &BufferRegion::set_y,
1630+
"set_y(y)");
1631+
16111632
add_varargs_method("to_string", &BufferRegion::to_string,
16121633
"to_string()");
16131634
add_varargs_method("to_string_argb", &BufferRegion::to_string_argb,

src/_backend_agg.h

+4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class BufferRegion : public Py::PythonExtension<BufferRegion> {
102102

103103
bool freemem;
104104

105+
// set the x and y corners of the rectangle
106+
Py::Object set_x(const Py::Tuple &args);
107+
Py::Object set_y(const Py::Tuple &args);
108+
105109
Py::Object to_string(const Py::Tuple &args);
106110
Py::Object to_string_argb(const Py::Tuple &args);
107111
static void init_type(void);

unit/memleak_hawaii3.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import os, sys, time, gc
44
import matplotlib
5-
matplotlib.use('Agg')
5+
matplotlib.use('PDF')
66

77
from matplotlib.cbook import report_memory
8-
import matplotlib.numerix as nx
8+
import numpy as np
99
from pylab import figure, show, close
1010

1111
# take a memory snapshot on indStart and compare it with indEnd
1212

13-
rand = nx.mlab.rand
13+
rand = np.mlab.rand
1414

1515
indStart, indEnd = 200, 401
1616
for i in range(indEnd):
@@ -19,8 +19,8 @@
1919
fig.clf()
2020

2121

22-
t1 = nx.arange(0.0, 2.0, 0.01)
23-
y1 = nx.sin(2*nx.pi*t1)
22+
t1 = np.arange(0.0, 2.0, 0.01)
23+
y1 = np.sin(2*np.pi*t1)
2424
y2 = rand(len(t1))
2525
X = rand(50,50)
2626

0 commit comments

Comments
 (0)