multiple colored plots in matlab

To create multiple colored plots in MATLAB, you can use the 'plot' function with different color specifications. There are several ways to specify colors in MATLAB:

  1. Specify color names: MATLAB provides a set of predefined color names that you can use directly in the 'plot' function. For example:
main.m
x = 1:10;
y1 = x;
y2 = x.^2;
y3 = sqrt(x);

plot(x, y1, 'r') % red line
hold on
plot(x, y2, 'g') % green line
plot(x, y3, 'b') % blue line
hold off
148 chars
11 lines
  1. Use RGB values: You can specify the color using RGB values, where each value ranges from 0 to 1. For example:
main.m
plot(x, y1, 'Color', [1 0 0]) % red line
hold on
plot(x, y2, 'Color', [0 1 0]) % green line
plot(x, y3, 'Color', [0 0 1]) % blue line
hold off
143 chars
6 lines
  1. Use hexadecimal color codes: You can also specify colors using hexadecimal color codes. For example:
main.m
plot(x, y1, 'Color', '#FF0000') % red line
hold on
plot(x, y2, 'Color', '#00FF00') % green line
plot(x, y3, 'Color', '#0000FF') % blue line
hold off
149 chars
6 lines

By using these color specification methods, you can create multiple colored plots in MATLAB.

Note: The 'hold on' and 'hold off' commands are used to enable/disable overwriting of previous plots in the same figure.

related categories

gistlibby LogSnag