% 例2-1:利用plot绘制不同效果的图形
x1 = -pi:.1:pi;
y1 = sin(x1);
subplot(2,3,1);plot(x1,y1);
title('plot绘制向量图');
x2 = magic(8);
subplot(2,3,2);plot(x2);
title('plot绘制向量图');
x3 = [3+2i,4+5i,5+7i,6+8i,7+9i,10+6i];
subplot(2,3,3);plot(x3);
title('plot绘制复数向量图');
x4 = 0.01:0.3:2*pi;
y2 = cos(x4 + 0.5) + 2;
subplot(2,3,4);plot(x4,y2,'r-.*');
title('给图形进行标识');
x5 = -pi:pi/10:pi;
y3 = tan(sin(x5)) - sin(tan(x5));
subplot(2,3,5);plot(x5,y3,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10);
title('图像进行属性设置');
x6 = -pi:pi/20:pi;
y4 = [sin(x6);sin(x6+1);sin(x6+2)];
z = [cos(x6);cos(x6+1);cos(x6+3)];
subplot(2,3,6);plot(x6,y4,'r:*',x6,z,'g-.v');
title('绘制多条曲线');
% 绘制对数坐标及半对数坐标图
clear all;
x1 = logspace(-1,2);
subplot(1,3,1);loglog(x1,exp(x1),'-s');
title('loglog函数绘图');
grid on;
x2 = 0:0.1:10;
subplot(1,3,2);semilogx(10.^x2,x2,'r-.*');
title('semilogx函数绘图');
subplot(1,3,3);semilogy(10.^x2,x2,'rd');
title('semilogy函数绘图');
当图像需要对具体数值更加清楚的展示时,为图形添加格栅是十分有效的。
% 为图形添加格栅
clear all;
x = linspace(0,10);
y = sin(x);
ax1 = subplot(2,1,1);
plot(x,y);
grid on;
y2 = sin(3*x);
ax2 = subplot(2,1,2);
plot(x,y2)
grid on;grid;
clear all
x = (0:0.1:2*pi)';
y1 = 2 * exp(-0.5 * x) * [1,-1];
y2 = 2 * exp(-0.5 * x) .* sin(2 * pi * x);
x1 = (0:12)/2;
y3 = 2 * exp(-0.5 * x1) .* sin(2*pi*x1);
plot(x,y1,':',x,y2,'--',x1,y3)
title('曲线及其包络线');
xlabel('变量X');ylabel('变量Y');
text(3.2,0.5,'包络线');
text(0.5,0.5,'曲线y');
text(1.4,0.15,'离散数据点');
legend('包络线','曲线y','离散数据点')
matlab提供了坐标轴控制函数axis。
clear all
t = 0.01:0.01:pi;
figure;
plot(sin(t),cos(t));
axis([-1 1 -2 2]) % 重新设置坐标轴
在已经存在的图上绘制新的曲线,并保留原来的曲线。
hold on:是当前的轴及图形保留下来而不被刷新,并接受即将绘制的新的曲线
hold off:为不保留当前轴及图形,绘制新的区先后,原图被刷新
hold:为hold on语句和hold off语句的切换
% 利用hold函数绘制迭绘图形
clear all
x = -pi:pi/20:pi;
plot(sin(x),'r:>');
hold on
plot(cos(x),'b-<');
hold off
matlab允许用户在同一个图形窗口中同时绘制多幅相互独立的子图,使用subplot函数。
% 利用subplot绘制子图
clear all
x = 0:0.01*pi:pi*16;
j = sqrt(-1);
subplot(2,2,1);plot(abs(sin(x)) .* (cos(x) + j * sin(x)),'LineWidth',3);
xlim([-1 1]);ylim([-1 1]);
subplot(2,2,2);plot(abs(sin(x/2)) .* (cos(x) + j * sin(x)),'LineWidth',3);
xlim([-1 1]);ylim([-1 1]);
subplot(2,2,3);plot(abs(sin(x/3)) .* (cos(x) + j * sin(x)),'LineWidth',3);
xlim([-1 1]);ylim([-1 1]);
subplot(2,2,4);plot(abs(sin(x/4)) .* (cos(x) + j * sin(x)),'LineWidth',3);
xlim([-1 1]);ylim([-1 1]);
matlab中设置了相应的鼠标操作的图形操作指令,分别是ginput、gtext和zoom函数。
ginput函数:
用于交互式从matlab绘制的图形中读取点的坐标。
clear all
x1 = 0:pi./100:2*pi;
plot(x1,cos(x1));
n = 10;
[x,y] = ginput(n);
>> x
x =
0.8548
1.3602
2.6075
3.9194
5.0269
5.3710
4.6075
4.2419
2.0806
2.7258
>> y
y =
0.6479
0.1887
-0.8735
-0.7335
0.2977
0.5856
-0.1381
-0.5272
-0.3755
0.2821
gtext函数:
gtext用于为图形添加交互式标记。
gtext('str'):用鼠标把字符串或字符串元胞数组放置到图形中作为文字说明
clear all
x = -pi:.1:pi;
y = sin(x);
plot(x,y);
xlabel('x');ylabel('y');
gtext('y = sin(x)','fontsize',10) % 添加文本
zoom函数:
zoom函数可以将局部图像进行放大。
clear all
t = 0.01:0.01:2*pi;
figure;
subplot(2,2,1);plot(t,sin(t));
axis([-5 10 -3 3]); % 设置坐标轴
title('放大前');
subplot(2,2,2);plot(t,sin(t));
axis([0 6 -1.5 1.5]);
zoom on; % 图像放大
title('放大后');
subplot(2,2,3);plot(t,sin(t));
axis([0 6 -3 3]);
zoom xon;
title('x轴方法');
subplot(2,2,4);plot(t,sin(t));
axis([-5 10 -1.5 1.5]);
zoom yon;
title('y轴放大');
在MATLAB中,提供了plotyy函数实现双坐标轴的绘制功能。
clear all
x = 0:0.01:20;
y1 = 200 * exp(-0.05 * x) .* sin(x);
y2 = 0.8 * exp(-0.5 * x) .* sin(10 * x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
xlabel('x');
set(get(AX(1),'Ylabel'),'String','慢衰减');
set(get(AX(2),'Ylabel'),'String','快衰减');
set(H1,'LineStyle','--')
set(H2,'LineStyle',':')
如果只知道函数的表达式,也可以绘制该函数的图形,函数fplot用于绘制一元函数的图形,函数explot用于 绘制二元函数的图形,函数ezploar用于绘制三元函数的图形。
fplot函数:
fplot函数根据函数的表达式自动调整自变量的范围,无须给函数赋值,直接生成或反映函数变化规律的图形,在函数变化快的区域,采用小的间隔,否则采用大的间隔。一般用在对横坐标取值间隔没有明确的要求,仅查看函数大致变化规律的情况下使用。
% 例2-12:利用fplot函数绘制f(x) = sin(tan(pi*x))
x = 0:0.01:1;
y = sin(tan(pi*x));
subplot(1,2,1);plot(x,y);
title('plot 函数绘图');
subplot(1,2,2);fplot('sin(tan(pi*x))',[0,1],1e-4);
title('fplot 函数绘图');
ezplot函数:
函数的表达式显示在图形的上方,同时对坐标轴可以不加任何限制作图。
subplot(2,1,1);
ezplot('cos(5 * t)','sin(3 * t)',[0,2 * pi])
grid on;
subplot(2,1,2);
ezplot('5 * x ^ 2 + 25 * y ^ 2 = 6',[-1.5,1.5,-1,1])
grid on;
条形图:
条形图可以显示适量数据和矩阵数据,如果用户需要表示跨时间段的运算结果、不同数据的比较结果以及部分相对整体比较结果,常会用到条形图绘制离散数据。
% 条形图
Y = [0.5 0.7 0.8;0.7 0.8 0.4;0.4 0.3 0.9;0.3 0.6 0.9;0.2 0.1 0.6];
subplot(2,2,1)
bar(Y,'grouped'),title 'Group';
xlabel('销售数据');ylabel('销售直方图')
subplot(2,2,2)
bar(Y,'stacked');title 'Stack'
xlabel('销售数据');ylabel('销售直方图')
subplot(2,2,3)
barh(Y,'stacked');title 'Stack'
xlabel('销售数据');ylabel('销售直方图')
subplot(2,2,4)
bar(Y,1.2);title 'Width = 1.2';
xlabel('销售数据');ylabel('销售直方图')
饼形图:
饼状图主要用于显示矩阵中每个元素在所有元素总和中所占的百分比及各部分之间的比例关系。
% 饼形图
x = [0.15 0.2 0.25 0.35];
pie(x)
直方图:
条形直方图中的x轴反映了数据y中元素数值的范围,直方图的y轴显示出参数y中的元素落入该组的数目
% 直方图
rand('state',1);
y = rand(100,1); % 生成待统计的数据
[n,x] = hist(y); % 返回统计频数n和区域中心位置x
s(1) = subplot(1,3,1);
hist(y); % 绘制统计直方图
xlabel('(a) hist(y)');
s(2) = subplot(1,3,2);
hist(y,7); % 绘制直方图并指定区域数目
xlabel('(b) hist(y,7)');
s(3) = subplot(1,3,3);
hist(y,0:.1:1); % 绘制直方图,并指定每个区域的中心位置
xlabel(' (x) hist(y,0:.1:1)');
axis(s,'square');
set(gcf,'Color','w');
散点图:
散点图将数据序列显示为一组点,在回归分析中较为常用,放映了因变量随自变量变化的趋势,便于观察两者关系。
% 散点图
x = linspace(0,3 * pi, 200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],x)
二维绘图的基本内容应该都在这里了,补充之间的绘图内容,接下来还会有三维绘图、四维绘图。