4
$\begingroup$

Let's say I want to use the following command to plot the following line segments defined by the points:

Graphics3D[Line[{{1, 1, -1}, {2, 2, 1}, {3, 3, -1}, {4, 4, 1}}]]

How do I make it so that each line segment has a different color? This is just an example but I would want to be able to do it with arbitrarily many points/line segments.

Thanks in advance!

$\endgroup$
1
  • 1
    $\begingroup$ It's not possible without breaking it up into several Line primitives. What you can do with a single line is assign a different colour to each vertex, and let the system interpolate between them along the segments. It's the VertexColors option. $\endgroup$
    – Szabolcs
    Commented Nov 7, 2016 at 19:34

1 Answer 1

8
$\begingroup$

A possible way:

points = {{1, 1, -1}, {2, 2, 1}, {3, 3, -1}, {4, 4, 1}};
lines = Line /@ Partition[points, 2, 1];
colors = RandomColor[Length[lines]];

Graphics3D[Transpose[{colors, lines}]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.