Skip to content

Commit 17ef13b

Browse files
authored
Merge pull request matplotlib#15910 from anntzer/eventplotinit
Simplify init of EventCollection.
2 parents 98ef7a6 + 799c7b9 commit 17ef13b

File tree

2 files changed

+16
-44
lines changed

2 files changed

+16
-44
lines changed

lib/matplotlib/collections.py

+14-44
Original file line numberDiff line numberDiff line change
@@ -1511,42 +1511,18 @@ def __init__(self,
15111511
--------
15121512
.. plot:: gallery/lines_bars_and_markers/eventcollection_demo.py
15131513
"""
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-
15401514
LineCollection.__init__(self,
1541-
segments,
1515+
[],
15421516
linewidths=linewidth,
15431517
colors=color,
15441518
antialiaseds=antialiased,
15451519
linestyles=linestyle,
15461520
**kwargs)
1547-
1521+
self._is_horizontal = True # Initial value, may be switched below.
15481522
self._linelength = linelength
15491523
self._lineoffset = lineoffset
1524+
self.set_orientation(orientation)
1525+
self.set_positions(positions)
15501526

15511527
def get_positions(self):
15521528
"""
@@ -1556,24 +1532,18 @@ def get_positions(self):
15561532
return [segment[0, pos] for segment in self.get_segments()]
15571533

15581534
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')
15651540
lineoffset = self.get_lineoffset()
15661541
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
15771547
self.set_segments(segments)
15781548

15791549
def add_positions(self, position):

lib/matplotlib/tests/test_collections.py

+2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ def test__EventCollection__add_positions():
101101
splt, coll, props = generate_EventCollection_plot()
102102
new_positions = np.hstack([props['positions'],
103103
props['extra_positions'][0]])
104+
coll.switch_orientation() # Test adding in the vertical orientation, too.
104105
coll.add_positions(props['extra_positions'][0])
106+
coll.switch_orientation()
105107
np.testing.assert_array_equal(new_positions, coll.get_positions())
106108
check_segments(coll,
107109
new_positions,

0 commit comments

Comments
 (0)