How to set color and line style of a plot in MATLAB?

In MATLAB, you can use the ‘Color’ parameter to set the color and the ‘LineStyle’ parameter to set the line style.

Setting Colors:
You can use predefined color names such as ‘red’, ‘blue’, ‘green’, etc.
Alternatively, you can use RGB color values, where [1,0,0] represents red, [0,0,1] represents blue, [0,1,0] represents green, etc.

Line style settings:
You can use predefined line style names, such as ‘solid’ for solid line, ‘dashed’ for dashed line, ‘dotted’ for dotted line, etc.
Alternatively, you can use character symbols to represent line styles, such as ‘-‘ for solid line, ‘–‘ for dashed line, ‘:’ for dotted line, etc.

Here is an example:

% 创建数据
x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = cos(x);

% 绘制图像
figure
hold on

% 绘制y1的图像,使用红色实线
plot(x, y1, 'Color', 'red', 'LineStyle', '-')

% 绘制y2的图像,使用蓝色虚线
plot(x, y2, 'Color', 'blue', 'LineStyle', '--')

% 添加图例
legend('y1', 'y2')

% 添加标题和坐标轴标签
title('Plot Example')
xlabel('x')
ylabel('y')

% 取消保持图像,使得下一个绘图不会覆盖当前图像
hold off

By running the above code, you will obtain an image containing two curves, with the y1 curve in solid red and the y2 curve in dashed blue.

bannerAds