To control the color of the line continuously, you will want to use surface .
At the first viewing, this function looks most useful for building three-dimensional surfaces; it provides more flexibility for coloring strings than the basic plot function. We can use the edges of the grid to build our line and use the colors of the C vertices to make an interpolated color along the edge.
You can check out the full list of rendering properties , but the ones you most likely want
- 'FaceColor', 'none', do not draw faces
- 'EdgeColor', 'interp', interpolate between vertices
Here is an example from MATLAB's Answer Message
x = 0:.05:2*pi; y = sin(x); z = zeros(size(x)); % We don't need a z-coordinate since we are plotting a 2d function C = cos(x); % This is the color, vary with x in this case. surface([x;x],[y;y],[z;z],[C;C],... 'FaceColor','none',... 'EdgeColor','interp');

source share