📜  MATLAB 中的二维线图

📅  最后修改于: 2022-05-13 01:54:48.846000             🧑  作者: Mango

MATLAB 中的二维线图

“2D”代表二维,二维线是在二维中移动的线。 2D 中的一条线意味着我们可以向前和向后移动,也可以向左、右、上、下等任何方向移动。

在 MATLAB 中,我们有一个名为plot()的函数,它允许我们在 2 个方向上绘制一条线。

句法:

现在让我们来看一些例子。

例 1:画一条简单的线:

MATLAB
% coordinates of the x-axis
x=[10,20,30,40,50]
 
% coordinates of the y-axis
y=[100,200,300,400,500]
 
% plot function is used to plot the
% line according to the coordinates
plot(x,y)
   
% to put grid on the graph
grid on


MATLAB
% coordinates of axis
y=[100,200,300,400,500]
 
% plot function is used to plot the
% line according to the coordinates
plot(x,y)
 
% to put grid on the graph
grid on


MATLAB
% coordinates of x-axis
x=[10,20,30,40,50]
 
% coordinates of y-axis of line 1
% represented by blue color
y1=[100,500,200,100,0]
 
% coordinates of y-axis of line 2
% represented by red color
y2=[400,100,0,200,300]
 
% coordinates of y-axis of line 3
% represented by yellow color
y3=[200,300,400,100,500]
 
% plot function to plot the lines on graph
plot(x,y1,x,y2,x,y3)
 
% to add grid on graph
grid on
 
% name of x axis
xlabel('x')
   
% name of y axis
ylabel('y')


MATLAB
% coordinates of x-axis
x=[1,2,3,4,5]
 
% coordinates of y-axis in form of matrix
% magic(n) matrix is a n*n matrix
% in which value scattered from 1 to n^2
% with equal row and columns sum
y=magic(5)
 
% plot function
plot(x,y)
 
% to add grid
grid on
 
% add name on axis
xlabel('x')
ylabel('y')


MATLAB
% coordinates of x-axis
x=[1,2,3,4,5]
 
% coordinates of y-axis
y=[50,40,30,20,10]
 
% assigning left side to the above
% coordinates
yyaxis left
 
% plot graph of left y-axis
plot(x,y)
   
% coordinates of y-axis
y=[10,20,30,40,50]
 
% assigning right side to the above
% coordinates
yyaxis right
 
% plot graph of right y-axis
plot(x,y)
 
% put grid on graph
grid on
 
% name of x-axis
xlabel('x')
   
% name of left side y coordinates
yyaxis left
ylabel('Left Side')
   
% name of right side y coordinates
yyaxis right
ylabel('Right Side')




输出 :

示例 2:绘制一条只有 1 个轴坐标作为输入的线:

注意:如果您只给出 1 个轴,则 plot()函数会将其作为 y 轴的坐标,并且默认情况下为 x 轴提供从 1、2、3 到 y 坐标的值。

MATLAB

% coordinates of axis
y=[100,200,300,400,500]
 
% plot function is used to plot the
% line according to the coordinates
plot(x,y)
 
% to put grid on the graph
grid on

输出 :

示例 3:在具有轴名称的同一图形上绘制 1 条以上的线:

MATLAB

% coordinates of x-axis
x=[10,20,30,40,50]
 
% coordinates of y-axis of line 1
% represented by blue color
y1=[100,500,200,100,0]
 
% coordinates of y-axis of line 2
% represented by red color
y2=[400,100,0,200,300]
 
% coordinates of y-axis of line 3
% represented by yellow color
y3=[200,300,400,100,500]
 
% plot function to plot the lines on graph
plot(x,y1,x,y2,x,y3)
 
% to add grid on graph
grid on
 
% name of x axis
xlabel('x')
   
% name of y axis
ylabel('y')



输出 :

示例 4:现在 y 轴的值作为矩阵而不是向量给出:

MATLAB

% coordinates of x-axis
x=[1,2,3,4,5]
 
% coordinates of y-axis in form of matrix
% magic(n) matrix is a n*n matrix
% in which value scattered from 1 to n^2
% with equal row and columns sum
y=magic(5)
 
% plot function
plot(x,y)
 
% to add grid
grid on
 
% add name on axis
xlabel('x')
ylabel('y')



输出:

示例 5:现在我们使用 2 y 轴绘制图形,一个在左侧,另一个在右侧。

MATLAB

% coordinates of x-axis
x=[1,2,3,4,5]
 
% coordinates of y-axis
y=[50,40,30,20,10]
 
% assigning left side to the above
% coordinates
yyaxis left
 
% plot graph of left y-axis
plot(x,y)
   
% coordinates of y-axis
y=[10,20,30,40,50]
 
% assigning right side to the above
% coordinates
yyaxis right
 
% plot graph of right y-axis
plot(x,y)
 
% put grid on graph
grid on
 
% name of x-axis
xlabel('x')
   
% name of left side y coordinates
yyaxis left
ylabel('Left Side')
   
% name of right side y coordinates
yyaxis right
ylabel('Right Side')

输出: