@@ -1511,42 +1511,18 @@ def __init__(self,
1511
1511
--------
1512
1512
.. plot:: gallery/lines_bars_and_markers/eventcollection_demo.py
1513
1513
"""
1514
- if positions is None:
1515
- raise ValueError('positions must be an array-like object')
1516
- # Force a copy of positions
1517
- positions = np.array(positions, copy=True)
1518
- segment = (lineoffset + linelength / 2.,
1519
- lineoffset - linelength / 2.)
1520
- if positions.size == 0:
1521
- segments = []
1522
- elif positions.ndim > 1:
1523
- raise ValueError('positions cannot be an array with more than '
1524
- 'one dimension.')
1525
- elif (orientation is None or orientation.lower() == 'none' or
1526
- orientation.lower() == 'horizontal'):
1527
- positions.sort()
1528
- segments = [[(coord1, coord2) for coord2 in segment] for
1529
- coord1 in positions]
1530
- self._is_horizontal = True
1531
- elif orientation.lower() == 'vertical':
1532
- positions.sort()
1533
- segments = [[(coord2, coord1) for coord2 in segment] for
1534
- coord1 in positions]
1535
- self._is_horizontal = False
1536
- else:
1537
- cbook._check_in_list(['horizontal', 'vertical'],
1538
- orientation=orientation)
1539
-
1540
1514
LineCollection.__init__(self,
1541
- segments ,
1515
+ [] ,
1542
1516
linewidths=linewidth,
1543
1517
colors=color,
1544
1518
antialiaseds=antialiased,
1545
1519
linestyles=linestyle,
1546
1520
**kwargs)
1547
-
1521
+ self._is_horizontal = True # Initial value, may be switched below.
1548
1522
self._linelength = linelength
1549
1523
self._lineoffset = lineoffset
1524
+ self.set_orientation(orientation)
1525
+ self.set_positions(positions)
1550
1526
1551
1527
def get_positions(self):
1552
1528
"""
@@ -1556,24 +1532,18 @@ def get_positions(self):
1556
1532
return [segment[0, pos] for segment in self.get_segments()]
1557
1533
1558
1534
def set_positions(self, positions):
1559
- """Set the positions of the events to the specified value."""
1560
- if positions is None or (hasattr(positions, 'len') and
1561
- len(positions) == 0):
1562
- self.set_segments([])
1563
- return
1564
-
1535
+ """Set the positions of the events."""
1536
+ if positions is None:
1537
+ positions = []
1538
+ if np.ndim(positions) != 1:
1539
+ raise ValueError('positions must be one-dimensional')
1565
1540
lineoffset = self.get_lineoffset()
1566
1541
linelength = self.get_linelength()
1567
- segment = (lineoffset + linelength / 2.,
1568
- lineoffset - linelength / 2.)
1569
- positions = np.asanyarray(positions)
1570
- positions.sort()
1571
- if self.is_horizontal():
1572
- segments = [[(coord1, coord2) for coord2 in segment] for
1573
- coord1 in positions]
1574
- else:
1575
- segments = [[(coord2, coord1) for coord2 in segment] for
1576
- coord1 in positions]
1542
+ pos_idx = 0 if self.is_horizontal() else 1
1543
+ segments = np.empty((len(positions), 2, 2))
1544
+ segments[:, :, pos_idx] = np.sort(positions)[:, None]
1545
+ segments[:, 0, 1 - pos_idx] = lineoffset + linelength / 2
1546
+ segments[:, 1, 1 - pos_idx] = lineoffset - linelength / 2
1577
1547
self.set_segments(segments)
1578
1548
1579
1549
def add_positions(self, position):
0 commit comments