MATLAB drawing must see, drawing Daquan! MATLAB Drawing Basic Operation Encyclopedia - Line Chart, Scatter Chart, Color Style, Line Thickness Summary

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


1. Complete collection of colors

color symbol corresponding color
r red
g green
b blue
c green
m Pink
y yellow
k black
w White

2. Complete Line Symbols

mark symbol corresponding color
plus
o hollow circle
‘*’ Asterisk
x cross
d diamond
^ upper triangle
p(pentagram) Pentagram
h(hexagram) hexagon
s(square) square
linear symbol meaning
- solid line (default)
double dash
: dotted line
:. Dotted line

2. Line chart drawing

The code is as follows (example):

x1 = [50 60 70 80 90 96];
y1_A = [1.0159 1.2227 1.2755 1.2845 1.4111 1.5173];
plot(x1,y1_A,'-*','color','b','MarkerSize',8);

The data requested by the url network used here.


Three, scatter plot drawing

Use the plot function without adding a linear symbol.
The code is as follows (example):

x1 = [50 60 70 80 90 96];
y1_A = [1.0159 1.2227 1.2755 1.2845 1.4111 1.5173];
plot(x1,y1_A,'*');

insert image description here

4. Line chart drawing

Just add a linear symbol, such as "-" on the basis of the scatter graph. To change the corresponding scatter graph into a line graph, you only need to add a line symbol in front of the marker symbol. The code is as follows (example)
:

x1 = [50 60 70 80 90 96];
y1_A = [1.0159 1.2227 1.2755 1.2845 1.4111 1.5173];
plot(x1,y1_A,'-*');

insert image description here

5. Add legend, title, coordinate axis (title)

1. Add legend
using statement

x1 = [50 60 70 80 90 96];
y1_A = [1.0159 1.2227 1.2755 1.2845 1.4111 1.5173];
y1_B = [1.7478 1.9648 2.1945 2.8580 3.0087 3.2728];
plot(x1,y1_A,'-*','color','b','MarkerSize',8);
hold on 
plot(x1,y1_B,'-o','color','g','MarkerSize',8);
legendText = legend('图例1','图例2','FontSize', 10,'FontName','宋体');

insert image description here
2. Add coordinates, specify coordinates

set(gca,'XLim',[-50 50]);        %X轴的数据显示范围
set(gca,'XTick',[-50:10:50]);    %设置要显示的坐标刻度
set(gca,'YLim',[0 100]);         %Y轴的数据显示范围
set(gca,'YTick',[0:10:100]);     %设置要显示的坐标刻度

3. Add picture title
After drawing a curve, you can use the title function to add a title to the drawing. The specific code format is as follows:

      plot(x, y, 'r')
      title('这是你绘制的曲线');

4. Add coordinate axis title

    xlabel(‘x轴的名称’);
    ylabel(‘y轴的名称’);
	axis([xmin xmax ymin ymax]); % 设置坐标轴在指定的区间
	%xmin、xmax 表示设置横坐标的最小最大值;
	%ymin、ymax 表示设置纵坐标的最小最大值。
	
	set(handles,‘xtick’,0100:2500) % handles可以指定具体坐标轴的句柄
	%功能: 设置X轴坐标范围02500,显示间隔是100
	
	set(handles,‘ytick’,0100:2500) % handles可以指定具体坐标轴的句柄
	%功能: 设置X轴坐标范围02500,显示间隔是100

6. Show or hide coordinate area grid when drawing

1. Display grid lines
Add the statement grid on code at the end of the drawing code
as follows (example):

	x = 1:10;
	y = rand(1,10);
	plot(x,y);
	grid on;  %显示网格语句

insert image description here

2. Display minor grid lines
Add the statements grid on and grid minor at the end of the drawing code
as follows (example):

	x = 1:10;
	y = rand(1,10);
	plot(x,y);
	grid on  %显示网格语句
	grid minor   %显示次网格语句

insert image description here

3. Do not display the grid lines
There are two methods for not displaying the grid
. The first method is to draw directly without adding any statement about grid lines, that is, without adding grid on and grid minor.

	x = 1:10;
	y = rand(1,10);
	plot(x,y);

In the second, add the statement grid off after the grid on and grid minor statements

	x = 1:10;
	y = rand(1,10);
	plot(x,y);
	grid on  %显示网格语句
	grid minor   %显示次网格语句
	grid off  %删除网格线

The above two methods can achieve the following effect
insert image description here
4. Display grid lines in a specific direction
By accessing the Axes object and setting the XGrid, YGrid and ZGrid properties, the grid lines can be displayed in a specific direction. These properties can be set to 'on' or 'off'. 'on' is displayed, 'off' is not displayed.
For example, create a 2D image and display gridlines only in the y direction.

	x = 1:10;
	y = rand(1,10);
	plot(x,y);
	ax = gca;
	ax.XGrid = 'off'; %off表示不显示网格线
	ax.YGrid = 'on';  %on表示显示网格线

The effect is as follows;
insert image description here

7. Draw multiple images and draw multiple pictures on the same picture

1. Draw multiple curves in the same graph
Use the statement hold on
code as follows (example):

    x1 = 1:10;
	y1 = rand(1,10);
    x2 = 1:10;
	y2 = rand(1,10);
	plot(x1,y1);  %绘制第一条曲线
    hold on    %继续在图像里绘制曲线
    plot(x2,y2);  %绘制第二条曲线

insert image description here

2. Draw multiple pictures
Use the statement figure(n), n indicates the number of figure windows to be generated
How to use: Add figure(n) before using the plot function to draw, which means to draw a curve in figure n, and the generated image is drawn The code named figure n
is as follows (example):

    x1 = 1:10;
	y1 = rand(1,10);
    x2 = 1:10;
	y2 = rand(1,10);
    figure(1) %第一张图片命名为figure 1
	plot(x1,y1);  %在图figure 1 中绘制曲线
    figure(2) %第二张图片命名为figure 2
    plot(x2,y2);  %在图figure 2 中绘制曲线

figure 1
insert image description here
figure 2
insert image description here

8. One graph draws multiple graphs, and multiple graphs are displayed in one graph window

Use the statement subplot(m,n,k), m represents the mth row, n represents the nth column, and k represents the kth figure from left to right from top to bottom For example: subplot
(2,2,4): means Create a multiple subplot with 2 rows and 2 columns, now draw the fourth plot (the plot of the second row and second column)

The code is as follows (example):

	x = [1 2 3 4 5];
	y = rand(4,5);
	subplot(2,2,1); %创建一个22列的多重子图,并取第1张开始绘画
	plot(x,y(1,:),'-o','color','b','MarkerSize',8);
	grid on    %添加图片网格
	subplot(2,2,2);  %绘制一个22列的多重子图,总计第2张图
	plot(x,y(2,:),'-o','color','r','MarkerSize',8);
	grid on
	subplot(2,2,3);  %绘制一个22列的多重子图,总计第2张图
	plot(x,y(3,:),'-o','color','b','MarkerSize',8);
	grid on
	subplot(2,2,4);  %绘制一个22列的多重子图,总计第2张图
	plot(x,y(4,:),'-o','color','r','MarkerSize',8);
	grid on

The result is as follows:
insert image description here
You can also add the name and title of the horizontal and vertical axes: (take the first picture as an example, the rest are similar)

	x = [1 2 3 4 5];
	y = rand(4,5);
	subplot(2,2,1); %创建一个22列的多重子图,并取第1张开始绘画
	plot(x,y(1,:),'-o','color','b','MarkerSize',8);
	xlabel("X轴名称");  %添加x轴名称
	ylabel("Y轴名称");  %添加y轴名称
	title('图片一图片一');  % 添加标题
	grid on    %添加图片网格
	subplot(2,2,2);  %绘制一个22列的多重子图,总计第2张图
	plot(x,y(2,:),'-o','color','r','MarkerSize',8);
	grid on
	subplot(2,2,3);  %绘制一个22列的多重子图,总计第2张图
	plot(x,y(3,:),'-o','color','b','MarkerSize',8);
	grid on
	subplot(2,2,4);  %绘制一个22列的多重子图,总计第2张图
	plot(x,y(4,:),'-o','color','r','MarkerSize',8);
	grid on

insert image description here

Summarize

This article gives the most basic overview of MATLAB drawing, including color, line type, plot symbol, scatter plot line chart drawing, adding picture title, legend, controlling coordinate range, adding or deleting grid lines to the image, etc.

reference

[1]https://blog.csdn.net/weixin_45839124/article/details/106439042?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168476007216800225570146%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=168476007216800225570146&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-1-106439042-null-null.142v87insert_down28,239v2insert_chatgpt&utm_term=MATLAB%E7%BB%98%E5%9B%BE%E6%B7%BB%E5%8A%A0%E6%A0%87%E9%A2%98&spm=1018.2226.3001.4187

[2]https://so.csdn.net/so/search?q=MATLAB%E7%BB%98%E5%9B%BE%E6%B7%BB%E5%8A%A0%E6%A0%87%E9%A2%98&t=&u=&urw=

Guess you like

Origin blog.csdn.net/qq_45296693/article/details/130814610