Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5570642

Browse files
committed
FIX: do not simplify path in LineCollection.get_segments
Internally all Collection flavors boil down to calling renderer.draw_path_collection and the sub-classes primarily provide nicer user-facing APIs to fabricate the paths that will be passed down to the renderer. In LineCollection rather than tracking both the user supplied data and the internal Path objects, we just keep the Path objects and re-extract segments on demand. To do this we use Path.iter_segments with defaults to asking the path if it should simplify the path (that is drop points that do not matter which is in turn defined by if the deflection away from "straight" is greater than some threshold). The Path objects we are holding have values in data-space, but the default value of "what is the threshold for 'not mattering'" is tuned to make sense in to pixel space (1/9). By passing `simplify=False` to `iter_segments` we over-ride the Path object's notion of if it should be simplified (which by default is controlled by the simplify threshold (rcParams['path.simplify_threshold']), rcParams['path.simplify'] , how long the path is, and if there are any quadratic or Bézier codes in the path) to never simplify. In this application we never want to simplify because we do not know enough of the context when this is called to know what the "right" simplification threshold is and because we expect `set_segments` and `get_segments` to round-trip. closes #20551
1 parent 06141da commit 5570642

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/matplotlib/collections.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,14 @@ def get_segments(self):
14611461
segments = []
14621462

14631463
for path in self._paths:
1464-
vertices = [vertex for vertex, _ in path.iter_segments()]
1464+
vertices = [
1465+
vertex
1466+
for vertex, _
1467+
# Never simplify here, we want to get the data-space values
1468+
# back and there in no way to know the "right" simplification
1469+
# threshold so never try.
1470+
in path.iter_segments(simplify=False)
1471+
]
14651472
vertices = np.asarray(vertices)
14661473
segments.append(vertices)
14671474

lib/matplotlib/tests/test_collections.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,3 +1039,12 @@ def test_quadmesh_cursor_data():
10391039
x, y = ax.transData.transform([-1, 101])
10401040
event = MouseEvent('motion_notify_event', fig.canvas, x, y)
10411041
assert qm.get_cursor_data(event) is None
1042+
1043+
1044+
def test_get_segments():
1045+
segments = np.tile(np.linspace(0, 1, 256), (2, 1)).T
1046+
lc = LineCollection([segments])
1047+
1048+
readback, = lc.get_segments()
1049+
# these should comeback un-changed!
1050+
assert np.all(segments == readback)

0 commit comments

Comments
 (0)