# plot a list color data is set by another list

I have a list of 10 values. I'm trying to plot the list using ListLinePlot, and using color function set by another list. The code is:

n = 9;
lis = Table[i, {i, 0, 1, 1/n}];
grad = {1, 9, 9, 9, 5, 5, 9, 9, 1, 1};
cf = Function[{x, f}, ColorData[{"AvocadoColors", {0, 9}}][f]];
ListLinePlot[lis, ColorFunction -> cf]


I have tried to set the color data for the values in grad, yet it does not seem to work. Also how can I plot the x-axis values as in the table?

EDIT - another problem:

I have a problem using interpolation when I'm trying to plot lis to scale. in this example:

n = 10;
lis = Table[i^4, {i, 0, 1, 1/n}];
grad = {1, 1, 9, 9, 9, 9, 9, 9, 10, 10, 10};
cf = Function[{x, f}, ColorData[{"AvocadoColors", {0, n}}][int[x]]];
g1 = ListLinePlot[lis, ColorFunction -> cf, ColorFunctionScaling -> False]
g2 = ListLinePlot[lis, ColorFunction -> cf2, ColorFunctionScaling -> False]
g3 = ListLinePlot[Table[{i, i^4}, {i, 0, 1, 1/n}], ColorFunction -> cf2, ColorFunctionScaling -> False]


g1 is color correctly, and for some reason g2 is not. when trying to plot the x axis to scale in g3 if I'm using cf it gives me errors for the interpolation function and using cf2 colors it in the wrong way as in g2. I'm not sure how to solve this.

I'm not 100% clear on what you want, but I assume that you want to actually plot grad on the y-axis, not lis. It also looks like you're trying to get "AvocadoColors" to scale from 0 to 9 instead of just the range of the data (1 to 9). If that's the case, my best guess at what you want is this:

n = 9;
lis = Subdivide[n];
grad = {1, 9, 9, 9, 5, 5, 9, 9, 1, 1};
cf = Function[{x, f}, ColorData[{"AvocadoColors", {0, 9}}][f]];
ListLinePlot[
ColorFunction -> cf,
ColorFunctionScaling -> False
]


Since f is localized in the Function, you don't actually use your Interpolation in the code so I removed it. The important bit is to set ColorFunctionScaling -> False since you're defining your own scale. Otherwise, it will always try to feed the ColorFunction a value between 0 and 1.

You can also define your colours using Slot. I find it easier to read myself, but it just depends on what style you prefer.

n = 9;
lis = Subdivide[n];
grad = {1, 9, 9, 9, 5, 5, 9, 9, 1, 1};
ListLinePlot[
ColorFunction -> (ColorData[{"AvocadoColors", {0, 9}}][#2] &),
ColorFunctionScaling -> False
]


For a 2D plot, slot #1 is x and slot #2 is f. For 3D plots you have x, y, and f. Some other plots have up to 5 possible numbers that can be delivered to ColorFunction. Either way the plot you get is:

EDIT 01:

Hopefully this will be closer to what you were looking for. I used the interpolation as you had it and just call it when I create cf.

n = 9;
lis = Table[i, {i, 0, 1, 1/n}];
grad = {1, 9, 9, 9, 5, 5, 9, 9, 1, 1};

• @jonathan I don't see why interpolation wouldn't work. I can pretty easily interpolate tens of millions of points so length shouldn't be a problem. You could use the VertexColors option of Line and create your own graphic. Something like Graphics[ Line[ Transpose[{Range[Length[lis]], lis}], VertexColors -> ColorData[{"AvocadoColors", {0, 9}}] /@ grad ], Frame -> True ] May 7 '21 at 21:21