TransWikia.com

How to use ListLinePlot to plot a multicolored line, where the colors are given by an array?

Mathematica Asked by Xiaoyu Liu on August 3, 2021

I have a 2D dataset with three columns. The first two columns are x and y, the third one is z. I want to plot a line with the colors specified by the z data.

Here is an ideal example, the only problem is that my dataset is not generated from a simple function.

data = Table[{x, Sin[x], Cos[x]}, {x, Range[-3.14, 3.14, 0.2]}]

I want to get a plot like this:
enter image description here

PS:
This picture is generated by

ListLinePlot[Table[{x, Sin[x]}, {x, Range[-[Pi], [Pi], 0.2]}], 
ColorFunction -> Function[{x, y}, Cos[x]], ColorFunctionScaling -> False]

But in reality I don’t have a function z(x,y).

One way I tried is to find the index of {x,y}, but sometimes it fails. I hope there is a better way to solve this.

One Answer

Just use Graphics directly instead of having ListLinePlot construct the Graphics object for you:

data = Table[{x, Sin[x], Cos[x]}, {x, Range[-3.14, 3.14, 0.2]}];
Graphics[
    {Line[data[[All, ;;2]], VertexColors -> data[[All,3]]]},
    Axes->True,
    AspectRatio->1/GoldenRatio
]

enter image description here

Correct answer by Carl Woll on August 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP