How to specify line colors for 2D plots created using arrays?
조회 수: 11 (최근 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!