How to specify line colors for 2D plots created using arrays?
19 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Raees Mohammed
il 4 Feb 2018
Commentato: Walter Roberson
il 6 Feb 2018
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 Commenti
Risposta accettata
Walter Roberson
il 4 Feb 2018
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 Commenti
Walter Roberson
il 6 Feb 2018
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]
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Line Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!