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

Skip to content

Commit e9380eb

Browse files
committed
cleand up reg poly collection and added example
svn path=/trunk/matplotlib/; revision=2299
1 parent 1ca997d commit e9380eb

5 files changed

Lines changed: 295 additions & 218 deletions

File tree

examples/dynamic_collection.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import random
2+
from matplotlib.colors import colorConverter
3+
from matplotlib.collections import RegularPolyCollection
4+
import matplotlib.cm as cm
5+
from pylab import figure, show, nx
6+
7+
fig = figure()
8+
ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
9+
ax.set_title("Press 'a' to add a point, 'd' to delete one")
10+
# a single point
11+
offsets = [(0.5,0.5)]
12+
facecolors = [cm.jet(0.5)]
13+
14+
collection = RegularPolyCollection(
15+
fig.dpi,
16+
numsides=5, # a pentagon
17+
rotation=0,
18+
sizes=(50,),
19+
facecolors = facecolors,
20+
edgecolors = (colorConverter.to_rgba('black'),),
21+
linewidths = (1,),
22+
offsets = offsets,
23+
transOffset = ax.transData,
24+
)
25+
26+
ax.add_collection(collection)
27+
28+
def onpress(event):
29+
"""
30+
press 'a' to add a random point from the collection, 'd' to delete one
31+
"""
32+
if event.key=='a':
33+
x,y = nx.mlab.rand(2)
34+
color = cm.jet(nx.mlab.rand())
35+
offsets.append((x,y))
36+
facecolors.append(color)
37+
fig.canvas.draw()
38+
elif event.key=='d':
39+
N = len(offsets)
40+
if N>0:
41+
ind = random.randint(0,N-1)
42+
offsets.pop(ind)
43+
facecolors.pop(ind)
44+
fig.canvas.draw()
45+
46+
fig.canvas.mpl_connect('key_press_event', onpress)
47+
48+
show()

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def show():
6363
"""
6464
Show all the figures and enter the gtk mainloop
6565
66-
This should be the last line of your script
66+
This should be the last line of your script. This function sets
67+
interactive mode to True, as detailed on
68+
http://matplotlib.sf.net/interactive.html
6769
"""
6870
for manager in Gcf.get_all_fig_managers():
6971
manager.show()

lib/matplotlib/collections.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,36 +288,66 @@ def __init__(self,
288288
sizes = (1,),
289289
**kwargs):
290290
"""
291-
Draw a regular polygon with numsides. sizes gives the area of
292-
the circle circumscribing the regular polygon and rotation is
293-
the rotation of the polygon in radians.
291+
Draw a regular polygon with numsides.
292+
293+
* dpi is the figure dpi instance, and is required to do the
294+
area scaling.
295+
296+
* numsides: the number of sides of the polygon
297+
298+
* sizes gives the area of the circle circumscribing the
299+
regular polygon in points^2
300+
301+
* rotation is the rotation of the polygon in radians
302+
303+
kwargs: See PatchCollection for more details
304+
305+
* offsets are a sequence of x,y tuples that give the centers of
306+
the polygon in data coordinates
307+
308+
* transOffset is the Transformation instance used to
309+
transform the centers onto the canvas.
310+
311+
Example: see examples/dynamic_collection.py for complete example
312+
313+
offsets = nx.mlab.rand(20,2)
314+
facecolors = [cm.jet(x) for x in nx.mlab.rand(20)]
315+
black = (0,0,0,1)
316+
317+
collection = RegularPolyCollection(
318+
fig.dpi,
319+
numsides=5, # a pentagon
320+
rotation=0,
321+
sizes=(50,),
322+
facecolors = facecolors,
323+
edgecolors = (black,),
324+
linewidths = (1,),
325+
offsets = offsets,
326+
transOffset = ax.transData,
327+
)
294328
295-
offsets are a sequence of x,y tuples that give the centers of
296-
the polygon in data coordinates, and transOffset is the
297-
Transformation instance used to transform the centers onto the
298-
canvas.
299329
300-
dpi is the figure dpi instance, and is required to do the area
301-
scaling.
302330
"""
303331
PatchCollection.__init__(self,**kwargs)
304-
self._sizes = asarray(sizes)
332+
self._sizes = sizes
305333
self._dpi = dpi
334+
self.numsides = numsides
335+
self.rotation = rotation
336+
self._update_verts()
306337

338+
def _update_verts(self):
307339
r = 1.0/math.sqrt(math.pi) # unit area
308-
309-
theta = (2*math.pi/numsides)*arange(numsides) + rotation
340+
theta = (2*math.pi/self.numsides)*arange(self.numsides) + self.rotation
310341
self._verts = zip( r*sin(theta), r*cos(theta) )
311342

312-
313-
314343
def draw(self, renderer):
315344
if not self.get_visible(): return
316345
renderer.open_group('regpolycollection')
317346
self._transform.freeze()
318347
self._transOffset.freeze()
319348
self.update_scalarmappable()
320-
scales = sqrt(self._sizes*self._dpi.get()/72.0)
349+
self._update_verts()
350+
scales = sqrt(asarray(self._sizes)*self._dpi.get()/72.0)
321351

322352
if is_string_like(self._edgecolors) and self._edgecolors == 'None':
323353
self._edgecolors = self._facecolors

0 commit comments

Comments
 (0)