The basic operation of drawing a line chart in matlab

This blog mainly summarizes the basic operations of matlab to draw a line graph. The function used is the plot function. The syntax rules of the plot function will not be described here. You can refer to the official matlab documentation, https://ww2.mathworks.cn/help/ matlab/ref/plot.html#d124e1037051, it doesn't matter if you don't read it, because I will summarize it in detail later.

To draw a line chart, first we need to set the line type, line color and marker , as shown in the following code:

x=1:1:10;%生成一个1-10范围内的增量为1的等间距行向量
y=[1 2 3 1 2 3 1 2 3 5];%生成一个行向量y
plot(x,y,'-vr');%以x作为横坐标,y作为纵坐标绘制折线图,其中-表示线型为实线,v表示标记为倒三角,r表示线条颜色为红色

The result of the operation is as follows:

insert image description here

The order in which you specify linetypes, colors and markers does not matter, in the above code, I first specify the line type, then specify the mark, and then specify the line color. I can specify it in another order. For example, in the following code, I first specify the mark, then specify the line color, and finally specify the line Type.

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
plot(x,y,'vr-');

The result is exactly the same as above:

insert image description here

If you do not specify a color, the default color will be used, if you do not specify a marker, there will be no marker, and if you do not specify a linetype, there will be no linetype.

Without specifying a linetype:

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
plot(x,y,'vr');

insert image description here

No flag specified:

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
plot(x,y,'-r');

insert image description here

The line types are as follows:

insert image description here
The color types are as follows:

insert image description hereThe tag types are as follows:

insert image description here

Set the line color, we can customize the RGB triplet, and adjust the line color to the color we want

x=1:1:10;%生成一个1-10范围内的增量为1的等间距行向量
y=[1 2 3 1 2 3 1 2 3 5];%生成一个行向量y
plot(x,y,'-v','color',[1,0.2,0.9]);%以x作为横坐标,y作为纵坐标绘制折线图,'color'表示设定线条颜色,其值就是RGB三元组,表示具体颜色三元组

The result of the operation is as follows:

insert image description here

Then, we need to set the line width, marker size, marker edge color and marker face color , as in the following code:

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
linewidth_line=1.5;%线宽1.5
markersize=5;%标记大小
plot(x,y,'-vb','linewidth',linewidth_line,...
  'markersize',markersize,'markerEdgeColor',[1,0,0],'markerfacecolor',[1,0,0]);%linewidth表示线宽,其后接具体值,makersize表示标记大小,其后接具体值,markerEdgeColor表示标记边缘颜色,其后接具体值,markerfacecolor表示标记颜色,其后接具体值	

The result of the operation is as follows:

insert image description here

If you do not set the edge color of the marker, its edge color will be the same as the line color

The result of the operation is as follows:

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
linewidth_line=1.5;%线宽1.5
markersize=5;%标记大小
plot(x,y,'-vg','linewidth',linewidth_line,...
  'markersize',markersize,'markerfacecolor',[1,0,0]);

insert image description here

Next, we set the axis labels, title, legend and font:

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
linewidth_line=1.5;%线宽1.5
markersize=5;%标记大小
plot(x,y,'-vb','linewidth',linewidth_line,...
  'markersize',markersize,'markerEdgeColor',[1,0,0],'markerfacecolor',[1,0,0]);
ylabel('Press/(Mpa)')%设置y轴标签
xlabel('Time/(s)')%设置x轴标签
legend('load 1')%设置图例
title('Transmation of the Load with the Time')%设置标题
set(gca,'FontName','Times New Roman','FontSize',12)%FontName:字体

The legend order is consistent with the plot order

The result of the operation is as follows:

insert image description here

Can be added legend box off;to turn off the legend border

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
linewidth_line=1.5;%线宽1.5
markersize=5;%标记大小
plot(x,y,'-vb','linewidth',linewidth_line,...
  'markersize',markersize,'markerEdgeColor',[1,0,0],'markerfacecolor',[1,0,0]);
ylabel('Press/(Mpa)')
xlabel('Time/(s)')
legend('load 1')
legend box off
title('Transmation of the Load with the Time')
set(gca,'FontName','Times New Roman','FontSize',12)%FontName:字体

The result of the operation is as follows:

insert image description here

can be added box offto turn off the axes outline

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
linewidth_line=1.5;%线宽1.5
markersize=5;%标记大小
plot(x,y,'-vb','linewidth',linewidth_line,...
  'markersize',markersize,'markerEdgeColor',[1,0,0],'markerfacecolor',[1,0,0]);
ylabel('Press/(Mpa)')
xlabel('Time/(s)')
legend('load 1')
legend box off
title('Transmation of the Load with the Time')
box off
set(gca,'FontName','Times New Roman','FontSize',12)%FontName:字体

The result of the operation is as follows:

insert image description here

Finally, let's set the size of the picture to make it more beautiful when inserted into a word document or PPT:

x=1:1:10;
y=[1 2 3 1 2 3 1 2 3 5];
linewidth_line=1.5;%线宽1.5
markersize=5;%标记大小
plot(x,y,'-vb','linewidth',linewidth_line,...
  'markersize',markersize,'markerEdgeColor',[1,0,0],'markerfacecolor',[1,0,0]);
ylabel('Press/(Mpa)')
xlabel('Time/(s)')
legend('load 1','load 2')
legend box off
title('Transmation of the Load with the Time')
set(gca,'FontName','Times New Roman','FontSize',12)%FontName:字体
box off;
set(gcf,'unit','centimeters','position',[5,5,12,9])
%gcf表示当前图形窗口,'unit'是单位设置,这里设置为'centimeters',
%'position'是图形窗口的位置和尺寸设置,其值为[5,5,12,9]
%[5,5,12,9]表示绘制的图像在页面上的宽度为12cm,高度为9cm,相对于页面左下角的位置为(5,5)

If we want to draw multiple line graphs, we only need to continue to use the plot function and combine hold on:

x=1:1:10;
y1=[1 2 3 1 2 3 1 2 3 5];
y2=[1 2 3 4 5 6 7 8 9 10];
linewidth_line=1.5;%线宽1.5
markersize=5;%标记大小
plot(x,y1,'-vb','linewidth',linewidth_line,...
  'markersize',markersize,'markerEdgeColor',[1,0,0],'markerfacecolor',[1,0,0]);
hold on
plot(x,y2,'-D','color',[48,151,164]/255,'linewidth',linewidth_line,...
  'markersize',markersize,'markerfacecolor',[48,151,164]/255);
ylabel('Press/(Mpa)')
xlabel('Time/(s)')
legend('load 1','load 2')
legend box off
title('Transmation of the Load with the Time')
set(gca,'FontName','Times New Roman','FontSize',12)%FontName:字体
box off;

insert image description here

If you do not use hold on, then the picture drawn later will cover the previous picture

x=1:1:10;
y1=[1 2 3 1 2 3 1 2 3 5];
y2=[1 2 3 4 5 6 7 8 9 10];
linewidth_line=1.5;%线宽1.5
markersize=5;%标记大小
plot(x,y1,'-vb','linewidth',linewidth_line,...
  'markersize',markersize,'markerEdgeColor',[1,0,0],'markerfacecolor',[1,0,0]);
plot(x,y2,'-D','color',[48,151,164]/255,'linewidth',linewidth_line,...
  'markersize',markersize,'markerfacecolor',[48,151,164]/255);
ylabel('Press/(Mpa)')
xlabel('Time/(s)')
legend('load 1','load 2')
legend box off
title('Transmation of the Load with the Time')
set(gca,'FontName','Times New Roman','FontSize',12)%FontName:字体
box off;

The result of the operation is as follows:

insert image description here

Finally, to make a summary, to draw a line chart, we need to set the line type, marker, line color, line width, marker size, marker edge color, marker face color, axis label, title, legend and font, and finally Then set the size of the picture. If you need to draw multiple pictures, you need to use it hold on, so that the picture drawn later will not cover the previous picture.

Guess you like

Origin blog.csdn.net/weixin_44049823/article/details/130261588