# How can different colours be assigned to lines? [closed]

Three sets of data generated from AnglePath3D and I'm stuck as to how to assign a different colour to each so they read better on screen. I'm not sure if I need to use another 3D plot function or just can't find the right graphics option?

Rasterize[
Graphics3D[
Line[#] &/@ {data1,data2,data3}, ColorRules->{data1->Red, data2->Blue, data3-
>Black},ViewPoint->Front ],
"Image"]


## closed as off-topic by MarcoB, LCarvalho, Edmund, gwr, J. M. will be back soon♦Oct 15 '17 at 13:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, LCarvalho, Edmund, gwr, J. M. will be back soon
If this question can be reworded to fit the rules in the help center, please edit the question.

• Under the "Neat Example" in the doc page for Line there are examples. Or you might look at the first example in the doc page for Graphics3D and see how different colored things are made and guess that each different thing could be replaced by a Line[..] object. – Michael E2 Oct 8 '17 at 2:10
• Also, read through the Structure of Graphics tutorial. – Szabolcs Oct 8 '17 at 11:39
• Ok. I've had time to try both now. I failed. The best I was able to do was make them different RandomColors. Is it worth updating the question with that? – BBirdsell Oct 8 '17 at 13:04

Since you don't tell us how you generated your data I will contrive some. The following generates three random walks in space, all starting the origin

SeedRandom[42];
Do[data[i] = Accumulate[Prepend[RandomReal[{-1, 1}, {10, 3}], {0, 0, 0}]], {i, 3}]


To give each walk its own color I will put each one in a list of form

{color, Line[points]}


To make these lists from the generated data points, I use Transpose.

lines = Transpose[{{Red, Blue, Black}, Table[Line @ data[i], {i, 3}]}];


Then the three data set can be visualized with the follow simple Graphics3D expression.

Graphics3D[{AbsoluteThickness[4], lines}]


In your case, you can replace

Table[Line @ data[i], {i, 3}]


with

Line /@ {data1, data2, data3}


### Update

This update addresses an issue raised by the OP in a comment to this answer.

Mathematica has so many functions for building and restructuring lists that problems involving list manipulation almost always have more than one approach. If you were to look at the question that are tagged (there are over 4000), you will see that many have quite a few solutions.

For example, I could have contrived my random walk data with FoldList rather than Accumulate and Prepend. I think many of Mathematica.SE regulars would argue using FoldList is more elegant.

SeedRandom[42];
{data1, data2, data3} =
Table[FoldList[Plus, {0, 0, 0}, RandomReal[{-1, 1}, {10, 3}]], 3];


Then we can write

 walks = Line /@ {data1, data2, data3};


and

 colors = {Red, Blue, Black};


Given these definitions, here are four ways of generating the random walk graphics.

 Graphics3D[{AbsoluteThickness[4], Transpose[{colors, walks}]}]
Graphics3D[{AbsoluteThickness[4], MapThread[List, {colors, walks}]}]
Graphics3D[{AbsoluteThickness[4], Inner[Sequence, colors, walks, List]}]


All four expressions generate exactly the same graphics as shown above in the pre-update section of this answer.

The first two approaches, the ones using Transpose and Thread, are the ones I recommend as good Mathematica practice. The other two work but are somewhat cowboy-ish -- they just show off that I recognize that MapThread and Inner, two functions really intended for other purposes, can be adapted to work for this problem by a careful choice of arguments.

You should also note that the three variables data1, data2, and data3 have been reduced to unnecessary intermediate variables by data generation method I use in this update. It would be better Mathematica practice to write

 SeedRandom[42];
walks =
Table[Line @ FoldList[Plus, {0, 0, 0}, RandomReal[{-1, 1}, {10, 3}]], 3];

• Wow. Thank you. I'm still wrapping my head around the structure of the MMA language. I played around with the Transpose step in my example and am wondering how you came up with that step? I can see what it does but there must be other ways to do that step? – BBirdsell Oct 9 '17 at 14:42
• @BBirdsell. I have made an update that I hope addresses the issues you raise in your comment. – m_goldberg Oct 10 '17 at 4:55
• Using Transpose feels wrong here, Thread seems more idiomatic. – LLlAMnYP Oct 11 '17 at 14:31
• @LLlAMnYP, personally I always used Transpose[] before things like Riffle[] came along. – J. M. will be back soon Oct 15 '17 at 13:50
• @J.M. the reason I think so is that Transpose is a "mathematical" function, while Thread feels "programmatic". I would not have been surprised if Transpose would not work on something like {{1,2,3},{{4,5},{6},{7,8,9}}} because of the raggedness of this array, but turns out it handles this gracefully. – LLlAMnYP Oct 16 '17 at 22:14