How to set different part of a line with differnet color?

17 views (last 30 days)
Hi all,
Suppose I want to draw a plot with command: plot(1:220, data, '.-'); The output line has a uniform color throughout the range. Now I hope to set the line color to blue for points have x-coordinate [1, 100], to red for points on [101, 150] and black on [151, 220]. How can I make it? Thanks a lot for your answer.

Answers (2)

dpb
dpb on 17 Sep 2016
Have to draw three lines; the color property is a single value for each line object. Also then note that to have continuous lines you'll have to include the bounding endpoints for each set of lines to include the last of the other group and that the line will switch color at the breakpoint. One way to do this would be to duplicate the data into three columns with NaN for the unwanted points in each column; then a single plot call will draw the three lines.

David Goodmanson
David Goodmanson on 17 Sep 2016
After you divide your data into three separate sets, use 'help plot' (fifth paragraph) to see how to put them on the same plot, and also how to select a separate line color for each part.
For multi-line plots, Matlab's automatic colors are much more attractive than they used to be. Unfortunately, the manual color options in the plot command still give you old-fashioned fully saturated colors, including a yellow that's hard to see, and a hideous bright green. This has been going on forever and should have been changed years ago. (Mathworks, are you listening?)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!