How to specify line colors for 2D plots created using arrays?
5 次查看(过去 30 天)
显示 更早的评论
This is the code I'm using to create shapes by plotting lines that overlap each other:
x = [10 16 10 10];
y = [12 10 8 12];
figure(1)
plot (x,y)
axis([0 100 0 100])
The issue I'm having is how to specify the line color for each line segment plotted or for the all the lines creating the shape. How do I specify the line color when each line is plotted using coordinates within arrays? I have a for-loop that creates a large amount of these shapes as a function.
0 个评论
采纳的回答
Walter Roberson
2018-2-4
This is not possible in MATLAB using plot() or related calls. plot() and related calls use line() objects, and line() objects are restricted to a single color.
You can break the vector into segments and draw the segments separately.
If you look in the File Exchange you will find a couple of different contributions for drawing colored lines.
2 个评论
Walter Roberson
2018-2-6
There are two ways:
- immediately after each coordinate list, you can give a character vector that is a "linespec", which are codes that define the line style, marker style, and line and marker color. For example, plot(x, y, 'r:*') defines a red dotted line with * marker. The color codes available for this purpose are b (blue), c (cyan), g (green), k (black), m (magenta), r (red), w (white), y (yellow) . If you give multiple coordinate sets in a single plot() call you can give one linespec for each coordinate set, such as plot(x, y1, 'r-', x, y2, 'b-') to make the first line red and the second line blue.
- Instead, after all of the coordinate lists and any corresponding linespec, you can use property/value pairs, including the 'Color' option. Any options given as property/value pairs apply to all of the lines created in that call. For example, plot(x, y1, x, y2, 'Color', 'r') would apply the color 'r' (red) to both lines. You can use the color codes I listed earlier, or you can specify RGB triples with values in the range 0 to 1, such as 'Color', [.9 .2 .5]
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!