Skip to content

Commit d2433ec

Browse files
committed
Simplify init of EventCollection.
Rely on set_positions to set up the relevant arrays, and also numpyfy it.
1 parent 8451629 commit d2433ec

File tree

1 file changed

+14
-44
lines changed

1 file changed

+14
-44
lines changed

lib/matplotlib/collections.py

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,42 +1509,18 @@ def __init__(self,
15091509
--------
15101510
.. plot:: gallery/lines_bars_and_markers/eventcollection_demo.py
15111511
"""
1512-
if positions is None:
1513-
raise ValueError('positions must be an array-like object')
1514-
# Force a copy of positions
1515-
positions = np.array(positions, copy=True)
1516-
segment = (lineoffset + linelength / 2.,
1517-
lineoffset - linelength / 2.)
1518-
if positions.size == 0:
1519-
segments = []
1520-
elif positions.ndim > 1:
1521-
raise ValueError('positions cannot be an array with more than '
1522-
'one dimension.')
1523-
elif (orientation is None or orientation.lower() == 'none' or
1524-
orientation.lower() == 'horizontal'):
1525-
positions.sort()
1526-
segments = [[(coord1, coord2) for coord2 in segment] for
1527-
coord1 in positions]
1528-
self._is_horizontal = True
1529-
elif orientation.lower() == 'vertical':
1530-
positions.sort()
1531-
segments = [[(coord2, coord1) for coord2 in segment] for
1532-
coord1 in positions]
1533-
self._is_horizontal = False
1534-
else:
1535-
cbook._check_in_list(['horizontal', 'vertical'],
1536-
orientation=orientation)
1537-
15381512
LineCollection.__init__(self,
1539-
segments,
1513+
[],
15401514
linewidths=linewidth,
15411515
colors=color,
15421516
antialiaseds=antialiased,
15431517
linestyles=linestyle,
15441518
**kwargs)
1545-
1519+
self._is_horizontal = True # Initial value, may be switched below.
15461520
self._linelength = linelength
15471521
self._lineoffset = lineoffset
1522+
self.set_positions(positions)
1523+
self.set_orientation(orientation)
15481524

15491525
def get_positions(self):
15501526
"""
@@ -1554,24 +1530,18 @@ def get_positions(self):
15541530
return [segment[0, pos] for segment in self.get_segments()]
15551531

15561532
def set_positions(self, positions):
1557-
"""Set the positions of the events to the specified value."""
1558-
if positions is None or (hasattr(positions, 'len') and
1559-
len(positions) == 0):
1560-
self.set_segments([])
1561-
return
1562-
1533+
"""Set the positions of the events."""
1534+
if positions is None:
1535+
positions = []
1536+
if np.ndim(positions) != 1:
1537+
raise ValueError('positions must be one-dimensional')
15631538
lineoffset = self.get_lineoffset()
15641539
linelength = self.get_linelength()
1565-
segment = (lineoffset + linelength / 2.,
1566-
lineoffset - linelength / 2.)
1567-
positions = np.asanyarray(positions)
1568-
positions.sort()
1569-
if self.is_horizontal():
1570-
segments = [[(coord1, coord2) for coord2 in segment] for
1571-
coord1 in positions]
1572-
else:
1573-
segments = [[(coord2, coord1) for coord2 in segment] for
1574-
coord1 in positions]
1540+
pos_idx = 0 if self.is_horizontal() else 1
1541+
segments = np.empty((len(positions), 2, 2))
1542+
segments[:, 0, pos_idx] = segments[:, 1, 0] = np.sort(positions)
1543+
segments[:, 0, 1 - pos_idx] = lineoffset + linelength / 2
1544+
segments[:, 1, 1 - pos_idx] = lineoffset - linelength / 2
15751545
self.set_segments(segments)
15761546

15771547
def add_positions(self, position):

0 commit comments

Comments
 (0)