绘图可视化:
1.简单绘图:
plot函数:
参数设置:
(1) (x,y): 以x为横坐标,y为纵坐标,按照(x,y)的顺序绘图。
(2)(y): y为以为实数数组,以1:n为横坐标,y为纵坐标绘图(n为y的长度)
(3)(X, Y, LineSpec): 用户指定的绘图样式
sin cos 图像(间隔0.02):
t=0.1:0.02:2*pi;
figure;
plot(t,sin(t),‘r:’);
hold on;
plot(t,cos(t),‘b:’);
xlabel(‘x’);
ylabel(‘y’);
title(‘plot’);
四行四列矩阵绘图:
A=[2 5 9 6 ; 5 8 6 3 ; 8 4 6 2; 8 9 3 5]
figure;
plot(A);
同时绘制多条曲线:
x=0:0.01:16;
y=sin(x);
z=cos(x);
figure;
plot(x,y,‘r:*’,x,z,‘g-.v’);
2.子图绘制和坐标轴控制:
(1)子图绘制:
subplot(m,n,p)函数:
m表示是图排成m行,n表示图排成n列,p表示图所在的位置。例如p=1表示从左到右从上到下的第一个位置。
x=0:0.01:16;
y=sin(x);
z=cos(x);
subplot(2,1,1);
plot(x,y,‘r–’);
subplot(2,1,2);
plot(x,z,‘g–’);
x=0:0.01:16;
y=sin(x);
z=cos(x);
s=tan(x);
subplot(2,2,1);
plot(x,y,‘r–’);
subplot(2,2,3);
plot(x,z,‘b–’);
subplot(2,2,[2,4]);
plot(x,z,‘g–’);
(2)坐标轴控制:
axis off: 取消坐标轴的显示。
axis( [xmin xmax ymin ymax] ):
可以设置当前坐标轴 x轴 和 y轴的限制范围。
axis( [xmin xmax ymin ymax zmin zmax cmin cmax] ) : 可以设置 x,y,z轴的限制范围和色差范围。
x=0:0.01:16;
y=sin(x);
plot(x,y,‘r–’);
axis([0 6 0 1]);
3.网格线和边框设置:
(1)网格线:
grid:
grid on; %显示网格线
grid off; %关闭网格线
x=0:0.01:16;
y=sin(x);
plot(x,y,‘r–’);
grid on;
(2)边框设置:
x=0:0.01:16;
y=sin(x);
plot(x,y,‘r–’);
box on;
4.中级技巧:
(1)拖拽
pan on;
x=0:0.01:16;
y=sin(x);
plot(x,y,‘r–’);
pan on;
(2)数据光标
datacursormode on;
x=0:0.01:16;
y=sin(x);
plot(x,y,‘r–’);
datacursormode on;
单击鼠标左键,即可获取数据点。
(3)极坐标绘图
polar(x,y,‘r–’); 参数:弧度、半径、线型
x=0:0.01:16;
y=sin(x);
polar(x,y,‘r–’);
(3)双Y轴绘图
plotyy(x,y,x,z);
x=0:0.01:16;
y=sin(x);
z=cos(x);
plotyy(x,y,x,z);
5.图例:
legend(‘sin(x)’,‘cos(x)’);
x=0:0.01:16;
y=sin(x);
z=cos(x);
plot(x,y,’-r’);
hold on;
plot(x,z,‘g-’);
axis([0 10 -1 1]);
legend(‘sin(x)’,‘cos(x)’);
6.文本标注
任意位置: gtext(‘y=sin(x)’);
x=0:0.01:16;
y=sin(x);
plot(x,y,’-r’);
gtext(‘y=sin(x)’);
7.各类二维图的绘制
(1) 直方图:
hist(x);
x=randn(100,1);
hist(x);
(2) 饼图:
pie(x,explode);
pie3();%可绘制三维饼图
x=[0.2 0.4 0.3];
subplot(121);
pie(x);
subplot(122);
y=[0.2 0.4 0.3 0.1];
explode([0 0 1 0]);
pie(y,explode);
(3) 散点图:
scatter(x,y);
scatter3();%可绘制三维散点图
x=[2 5 6 3 8 9 5 ];
y=[ 8 9 5 1 6 6 5 ];
scatter(x,y,[ ],[1 0 0],‘fill’);
8.三维图的绘制:
x=-10:0.1:10;
y=-10:0.1:10;
[X,Y]=meshgrid(x,y);
z=X.^ 2 + Y.^2;
surf(x,y,z);
colormap(‘cool’);
axis square;
view([55,75]);
xlabel(‘x’);
ylabel(‘y’);
zlabel(‘z’);