21
21
from matplotlib .collections import LineCollection
22
22
23
23
24
- def colored_line (x , y , c , ax = None , scalex = True , scaley = True , ** lc_kwargs ):
24
+ def colored_line (x , y , c , ax = None , ** lc_kwargs ):
25
25
"""
26
26
Plot a line with a color specified along the line by a third value.
27
27
@@ -38,10 +38,7 @@ def colored_line(x, y, c, ax=None, scalex=True, scaley=True, **lc_kwargs):
38
38
The color values, which should be the same size as x and y.
39
39
ax : matplotlib.axes.Axes, optional
40
40
The axes to plot on. If not provided, the current axes will be used.
41
- scalex, scaley : bool
42
- These parameters determine if the view limits are adapted to the data limits.
43
- The values are passed on to autoscale_view.
44
- **lc_kwargs : Any
41
+ **lc_kwargs
45
42
Any additional arguments to pass to matplotlib.collections.LineCollection
46
43
constructor. This should not include the array keyword argument because
47
44
that is set to the color argument. If provided, it will be overridden.
@@ -63,13 +60,11 @@ def colored_line(x, y, c, ax=None, scalex=True, scaley=True, **lc_kwargs):
63
60
(xy [0 , :][None , :], (xy [:- 1 , :] + xy [1 :, :]) / 2 , xy [- 1 , :][None , :]), axis = 0
64
61
)
65
62
segments = np .stack ((xy_mid [:- 1 , :], xy , xy_mid [1 :, :]), axis = - 2 )
66
- # Note that segments is [
67
- # [[x[0], y[0]], [x[0], y[0]], [mean(x[0], x[1]), mean(y[0], y[1])]],
68
- # [[mean(x[0], x[1]), mean(y[0], y[1])], [x[1], y[1]],
69
- # [mean(x[1], x[2]), mean(y[1], y[2])]],
70
- # ...
71
- # [[mean(x[-2], x[-1]), mean(y[-2], y[-1])], [x[-1], y[-1]], [x[-1], y[-1]]]
72
- # ]
63
+ # Note that
64
+ # segments[0, :, :] is [xy[0, :], xy[0, :], (xy[0, :] + xy[1, :]) / 2]
65
+ # segments[i, :, :] is [(xy[i - 1, :] + xy[i, :]) / 2, xy[i, :],
66
+ # (xy[i, :] + xy[i + 1, :]) / 2] if i not in {0, len(x) - 1}
67
+ # segments[-1, :, :] is [(xy[-2, :] + xy[-1, :]) / 2, xy[-1, :], xy[-1, :]]
73
68
74
69
lc_kwargs ["array" ] = c
75
70
lc = LineCollection (segments , ** lc_kwargs )
@@ -79,7 +74,6 @@ def colored_line(x, y, c, ax=None, scalex=True, scaley=True, **lc_kwargs):
79
74
ax .add_collection (lc )
80
75
ax .autoscale_view (scalex = scalex , scaley = scaley )
81
76
82
- # Return the LineCollection object
83
77
return lc
84
78
85
79
0 commit comments