How to specify line colors for 2D plots created using arrays?

9 ビュー (過去 30 日間)
Raees Mohammed
Raees Mohammed 2018 年 2 月 4 日
コメント済み: Walter Roberson 2018 年 2 月 6 日
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.

採用された回答

Walter Roberson
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 件のコメント
Raees Mohammed
Raees Mohammed 2018 年 2 月 4 日
Is it possible to specify what that single color is? I'm using the code above in a for to create a series of shapes. When the code is used with the for-loop, it creates the series of shapes, but assigns each a different color. I want to be able to at least keep those colors constant.
Walter Roberson
Walter Roberson 2018 年 2 月 6 日
There are two ways:
  1. 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.
  2. 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 ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by