1. Basic form
plot(X,Y,LineSpec)
The plot function is used to draw a two-dimensional image with X as the abscissa and Y as the ordinate, which can be a scatter plot or a line graph. X and Y are arrays with the same length, and LineSpec is the custom settings of the graphics: line type, color, and mark, which are expressed in single quotation marks at once.
r | g | b | y |
red | green | blue | yellow |
k | w | c | m |
black | White | Green | pink |
- | -- | : | -. |
solid line (default) | long dashed line | dashed line | Dotted line |
+ | o | * | . |
plus | hollow circle | Asterisk | solid circle |
^ | v | < | > |
upper triangle | lower triangle | left triangle | right triangle |
x | s | d | p |
Cross | square | diamond | Pentagram |
h | |||
hexagon |
In addition to the above three types of attributes, LineSpec can also customize other parameters of graphics. When applying, you must first write the parameter name within single quotes, and then write the parameter value, separated by commas.
parameter name | meaning | Parameter Type |
LineWidth | line width | number |
MarkerSize | data point size | number |
MarkerEdgeColor | Data point border line color | marker |
MarkerFaceColor | Fill color of the area inside the marker point | marker |
2. Figure interface settings
You can also use statements to define attributes such as legend, axis label, and graph name, as follows:
title('图像名'); %定义图像名称
legend('曲线1','曲线2'); %定义图例名称
axis tight; %坐标轴范围自动紧凑
axis([xmin xmax ymin ymax]); %自定义x、y轴绘图区间,xmin等替换为数字即可
axis equal; %图像变为正方形,拉伸缩小不改变形状
set(gca,'Box','off'); %去除图像右侧和上侧的坐标线
grid on; %显示网格线
set(gca,'XGrid','on'); %仅显示X轴网格
set(gca,'YGrid','on'); %仅显示Y轴网格
xlabel('x轴名称'); ylabel('y轴名称'); %定义x、y轴的标签
set(gca,'FontName','黑体','FontSize',14); %定义图像的字体、字号
set(gca,'linewidth',0.5); %定义坐标轴线粗
set(get(gca,'XLabel'),'FontSize',8); %仅将x轴字号设为8号,其余默认
set(get(gca,'TITLE'),'FontSize',8); %仅将标题字号设为8号,其余默认
hold on; %保留图像,即之后还有plot函数时,不新建figure窗口,直接增添在当前图像上
3. Example
To draw a blue long dotted line with data points as hollow circles, set the corresponding x and y axis labels, legend, modify the font size and other attributes, the statement and execution results are as follows:
x = [0.9181,1.1006,1.2803,1.4574,1.6317,1.8033,1.9721,2.1382,2.3016,2.4623];
y = [0.1108,0.1306,0.1529,0.1805,0.1972,0.2181,0.2412,0.2601,0.2864,0.3195];
plot(x,y,'b--o','MarkerSize',8);
xlabel('通流面积A(mm^2)');ylabel('质量流量Qm(kg/s)');
legend('流量变化曲线');
axis([0.5 3 0.05 0.35]);
set(gca,'FontSize',14,'Box','off');
4. Three-dimensional image rendering
If you encounter a three-dimensional graph with a binary function, there are two methods:
(1) The plot3 function, usage and markers are the same as the plot function.
(2) mesh function, usage is similar to plot. The mesh function can color the value of the binary function according to the size, which is more convenient for observation.
Example statements and running results are as follows:
x=9:0.1:13;
y=281:0.1:300;
[X,Y]=meshgrid(x,y); %将x,y向量转为二维矩阵
z=-2017.75-68.23.*X+17.48.*Y-0.1022.*X.^2+0.2447.*X.*Y-0.03308.*Y.^2;
mesh(X,Y,z);
hold on;
plot3(Pin,Tin,Th,'.k','MarkerSize',20); %实验数据,用黑色实心圆点表示