Skip to content

Commit 86774fc

Browse files
committed
scatter() now supports empty arrays as input (i.e., scatter([], []) is now valid).
Enabling this required fixes in both scatter() and in collections.py. Collections now support empty versions of themselves more correctly due to these fixes. svn path=/trunk/matplotlib/; revision=8988
1 parent 62d4178 commit 86774fc

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2011-02-18 scatter([], []) is now valid. Also fixed issues
2+
with empty collections - BVR
3+
14
2011-02-07 Quick workaround for dviread bug #3175113 - JKS
25

36
2011-02-05 Add cbook memory monitoring for Windows, using

lib/matplotlib/axes.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5832,24 +5832,17 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
58325832
else:
58335833
collection.autoscale_None()
58345834

5835-
temp_x = x
5836-
temp_y = y
5837-
5838-
minx = np.amin(temp_x)
5839-
maxx = np.amax(temp_x)
5840-
miny = np.amin(temp_y)
5841-
maxy = np.amax(temp_y)
5842-
5843-
w = maxx-minx
5844-
h = maxy-miny
5845-
58465835
# the pad is a little hack to deal with the fact that we don't
58475836
# want to transform all the symbols whose scales are in points
58485837
# to data coords to get the exact bounding box for efficiency
58495838
# reasons. It can be done right if this is deemed important
5850-
padx, pady = 0.05*w, 0.05*h
5851-
corners = (minx-padx, miny-pady), (maxx+padx, maxy+pady)
5852-
self.update_datalim( corners)
5839+
# Also, only bother with this padding if there is anything to draw.
5840+
if self._xmargin < 0.05 and x.size > 0 :
5841+
self.set_xmargin(0.05)
5842+
5843+
if self._ymargin < 0.05 and x.size > 0 :
5844+
self.set_ymargin(0.05)
5845+
58535846
self.autoscale_view()
58545847

58555848
# add the collection last

lib/matplotlib/collections.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class Collection(artist.Artist, cm.ScalarMappable):
5959
scalar mappable will be made to set the face colors.
6060
"""
6161
_offsets = np.array([], np.float_)
62+
# _offsets must be a Nx2 array!
63+
_offsets.shape = (0, 2)
6264
_transOffset = transforms.IdentityTransform()
6365
_transforms = []
6466

@@ -95,10 +97,11 @@ def __init__(self,
9597

9698
self._uniform_offsets = None
9799
self._offsets = np.array([], np.float_)
100+
# Force _offsets to be Nx2
101+
self._offsets.shape = (0, 2)
98102
if offsets is not None:
99103
offsets = np.asarray(offsets)
100-
if len(offsets.shape) == 1:
101-
offsets = offsets[np.newaxis,:] # Make it Nx2.
104+
offsets.shape = (-1, 2) # Make it Nx2
102105
if transOffset is not None:
103106
self._offsets = offsets
104107
self._transOffset = transOffset
@@ -148,13 +151,17 @@ def get_datalim(self, transData):
148151
transOffset = self._transOffset
149152
offsets = self._offsets
150153
paths = self.get_paths()
154+
155+
151156
if not transform.is_affine:
152157
paths = [transform.transform_path_non_affine(p) for p in paths]
153158
transform = transform.get_affine()
154159
if not transOffset.is_affine:
155160
offsets = transOffset.transform_non_affine(offsets)
156161
transOffset = transOffset.get_affine()
162+
157163
offsets = np.asarray(offsets, np.float_)
164+
offsets.shape = (-1, 2) # Make it Nx2
158165

159166
result = mpath.get_path_collection_extents(
160167
transform.frozen(), paths, self.get_transforms(),
@@ -176,6 +183,7 @@ def _prepare_points(self):
176183
offsets = self._offsets
177184
paths = self.get_paths()
178185

186+
179187
if self.have_units():
180188
paths = []
181189
for path in self.get_paths():
@@ -184,17 +192,19 @@ def _prepare_points(self):
184192
xs = self.convert_xunits(xs)
185193
ys = self.convert_yunits(ys)
186194
paths.append(mpath.Path(zip(xs, ys), path.codes))
187-
if len(self._offsets):
188-
xs = self.convert_xunits(self._offsets[:,0])
189-
ys = self.convert_yunits(self._offsets[:,1])
195+
196+
if offsets.size > 0:
197+
xs = self.convert_xunits(offsets[:,0])
198+
ys = self.convert_yunits(offsets[:,1])
190199
offsets = zip(xs, ys)
191200

192201
offsets = np.asarray(offsets, np.float_)
202+
offsets.shape = (-1, 2) # Make it Nx2
193203

194204
if not transform.is_affine:
195205
paths = [transform.transform_path_non_affine(path) for path in paths]
196206
transform = transform.get_affine()
197-
if not transOffset.is_affine:
207+
if not transOffset.is_affine :
198208
offsets = transOffset.transform_non_affine(offsets)
199209
transOffset = transOffset.get_affine()
200210

@@ -258,8 +268,7 @@ def set_offsets(self, offsets):
258268
ACCEPTS: float or sequence of floats
259269
"""
260270
offsets = np.asarray(offsets, np.float_)
261-
if len(offsets.shape) == 1:
262-
offsets = offsets[np.newaxis,:] # Make it Nx2.
271+
offsets.shape = (-1, 2) # Make it Nx2
263272
#This decision is based on how they are initialized above
264273
if self._uniform_offsets is None:
265274
self._offsets = offsets
@@ -1221,6 +1230,7 @@ def draw(self, renderer):
12211230
offsets = zip(xs, ys)
12221231

12231232
offsets = np.asarray(offsets, np.float_)
1233+
offsets.shape = (-1, 2) # Make it Nx2
12241234

12251235
self.update_scalarmappable()
12261236

0 commit comments

Comments
 (0)