Effacer les filtres
Effacer les filtres

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

10 vues (au cours des 30 derniers jours)
Raees Mohammed
Raees Mohammed le 4 Fév 2018
Commenté : Walter Roberson le 6 Fév 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 4 Fév 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 commentaires
Raees Mohammed
Raees Mohammed le 4 Fév 2018
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 le 6 Fév 2018
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]

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by