{"record":{"id":"8e2c8cc1dc7e91ca","repo":"matplotlib/matplotlib","slug":"positions-must-be-one-dimensional","errorCode":null,"errorMessage":"positions must be one-dimensional","messagePattern":"positions must be one-dimensional","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/collections.py","lineNumber":1969,"sourceCode":"        self._is_horizontal = True  # Initial value, may be switched below.\n        self._linelength = linelength\n        self._lineoffset = lineoffset\n        self.set_orientation(orientation)\n        self.set_positions(positions)\n\n    def get_positions(self):\n        \"\"\"\n        Return an array containing the floating-point values of the positions.\n        \"\"\"\n        pos = 0 if self.is_horizontal() else 1\n        return [segment[0, pos] for segment in self.get_segments()]\n\n    def set_positions(self, positions):\n        \"\"\"Set the positions of the events.\"\"\"\n        if positions is None:\n            positions = []\n        if np.ndim(positions) != 1:\n            raise ValueError('positions must be one-dimensional')\n        lineoffset = self.get_lineoffset()\n        linelength = self.get_linelength()\n        pos_idx = 0 if self.is_horizontal() else 1\n        segments = np.empty((len(positions), 2, 2))\n        segments[:, :, pos_idx] = np.sort(positions)[:, None]\n        segments[:, 0, 1 - pos_idx] = lineoffset + linelength / 2\n        segments[:, 1, 1 - pos_idx] = lineoffset - linelength / 2\n        self.set_segments(segments)\n\n    def add_positions(self, position):\n        \"\"\"Add one or more events at the specified positions.\"\"\"\n        if position is None or (hasattr(position, 'len') and\n                                len(position) == 0):\n            return\n        positions = self.get_positions()\n        positions = np.hstack([positions, np.asanyarray(position)])\n        self.set_positions(positions)\n    extend_positions = append_positions = add_positions","sourceCodeStart":1951,"sourceCodeEnd":1987,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/collections.py#L1951-L1987","documentation":"EventCollection (the artist behind ax.eventplot) stores each event as a short segment at a position along one axis; set_positions requires a flat, 1-dimensional sequence of positions. Any nested list or 2D array (np.ndim != 1) raises ValueError before the segments are built.","triggerScenarios":"EventCollection([[1, 2], [3, 4]]); ev.set_positions(np.array([[0.1], [0.5]])) (an (N,1) column array); passing per-event [pos, weight] pairs.","commonSituations":"Positions arriving from grouped/aggregated data as a list of single-element lists; column vectors from pandas or sklearn; converting event data that was stored nested.","solutions":["Flatten first: ev.set_positions(np.ravel(positions))","Pass a flat Python list or 1D ndarray of positions","For multi-row eventplots, keep a list of 1D arrays at the eventplot() call level rather than nesting into set_positions"],"exampleFix":"# before\nev = EventCollection([[0.1], [0.4], [0.9]])  # each wrapped in a list\n\n# after\nev = EventCollection([0.1, 0.4, 0.9])\n# or: ev.set_positions(np.ravel(positions))","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef set_positions_1d(ev, positions):\n    pos = np.asarray(positions)\n    if pos.ndim != 1:\n        pos = pos.ravel()\n    ev.set_positions(pos)\n\nset_positions_1d(event_collection, nested_positions)","typeGuard":"import numpy as np\n\ndef is_flat_positions(p) -> bool:\n    return np.ndim(p) == 1","tryCatchPattern":null,"preventionTips":["ravel positions before set_positions","eventplot wants a list of 1D arrays (one per row) at the call level — do not nest single events","Validate ndim == 1 in data loaders that feed event plots"],"tags":["matplotlib","eventplot","positions","dimensionality","valueerror"],"backgroundTag":"array-dimensionality-mismatch","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}