MATLAB Answers

Bob Choy

Coloring the lines in a a graph

Asked by Bob Choy
on 18 Nov 2012

I have a graph with 18 horizontal lines, I need to first 6 lines to be colored magenta, the following 6 to be cyan and the last 6 to be red. Then I want to add a small label to that graph saying something like [0-6 = A], [6-12 = B] and [12-18 = C].

Is this possible?

Thank you.




No products are associated with this question.

1 Answer

Answer by Image Analyst
on 18 Nov 2012

Yes. Use the 'Color' option in line()

x1 = 0;
x2 = 20;
for y = 1 : 6
	line([x1 x2], [y y], 'Color', 'm');
	hold on;
for y = 7 : 12
	line([x1 x2], [y y], 'Color', 'c');
for y = 13 : 18
	line([x1 x2], [y y], 'Color', 'r');


Can you please elaborate? More especifically, what should I be doing? What is x1 and x2?

The above is fairly specific. line() is a MATLAB plotting call. Calling it with the parameter/value pairs shown will give you magenta or cyan or read lines.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!