1.Logarithm Plots(对数绘图)
x = logspace(-1,1,100);
y = x .^ 2;
subplot(2,2,1);
plot(x,y);
title('Plot');
subplot(2,2,2);
semilogx(x,y);
title('Semilogx');
subplot(2,2,3);
semilogy(x,y);
title('Semilogy');
subplot(2,2,4);
loglog(x,y);
title('Loglog');
set(gca,'Xgrid','on');
如,定义一个这样的函数: y = a e − b x s i n ( c x ) y=ae^{-bx}sin(cx) y=ae−bxsin(cx):
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);
set(get(AX(1),'Ylabel'),'String','Left Y-axis');
set(get(AX(2),'Ylabel'),'String','Right Y-axis');
title('Labeling plotyy');
set(H1,'LineStyle','--');
set(H2,'LineStyle',':');
y = randn(1,1000);
subplot(2,1,1);
hist(y,10);
title('Bins = 10');
subplot(2,1,2);
hist(y,50);
title('Bins = 50');
x = [1 2 5 4 8];
y = [x;1:5];
subplot(1,3,1);bar(x);title('A bargraph of vector x');
subplot(1,3,2);bar(y);title('A bargraph of vector y');
subplot(1,3,3);bar3(y);title('A 3D bargraph');
x = [1 2 5 4 8];
y = [x;1:5];
subplot(1,2,1);bar(y,'stacked');title('Stacked');
subplot(1,2,2);barh(y);title('Horizontal');
x = [1 2 5 4 8];
y = [x;1:5];
subplot(1,3,1);bar(y,'stacked');title('Stacked');
subplot(1,3,2);barh(y);title('Horizontal');
subplot(1,3,3);barh(y,'stacked');title('Horizontal Stacked');
a = [10 5 20 30];
subplot(2,2,1); pie(a);
subplot(2,2,2); pie(a , [0,0,0,1]);
subplot(2,2,3); pie(a , [1,1,1,1]);
subplot(2,2,4); pie3(a, [0,0,0,1]);
x = 1:100;
theta = x/10;
r = log10(x);
subplot(2,3,1); polar(theta,r);
theta = linspace(0 , 2 * pi);
r = cos(4 * theta);
subplot(2,3,2); polar(theta,r);
theta = linspace(0 , 2 * pi , 6);
r = ones(1 , length(theta));
subplot(2,3,3); polar(theta,r);
theta = linspace(0 , 2 * pi);
r = 1 - sin(theta);
subplot(2,3,4); polar(theta,r);
theta = linspace(0 , 2 * pi , 7);
r = ones(1 , length(theta));
subplot(2,3,5); polar(theta,r);
theta = linspace(0 , 2 * pi , 10);
r = ones(1 , length(theta));
subplot(2,3,6); polar(theta,r);
6.Stairs(阶梯图) 和 Stem(针状图) Charts
x = linspace(0 , 4 * pi , 40);
y = sin(x);
subplot(1,2,1); stairs(y);
subplot(1,2,2); stem(y);
t=[0:0.2:10];
f=sin((pi * t .^ 2) ./ 4);
h=stem(t,f,'-r');
set(h,'markerEdgecolor','red');
hold on
t2=[0:0.01:10];
f2=sin((pi * t2 .^ 2) ./ 4);
plot(t2,f2,'b');
yticks([-1:0.5:1]);
7.Boxplot(箱线图) 和 Error Bar(含误差条的线图)
load carsmall
% 根据样本数据创建每加仑英里数 (MPG) 测量值的箱线图,按车辆的原产国 (Origin) 分组
subplot(1,2,1); boxplot(MPG , Origin);
x = 0:pi/10:pi;
y = sin(x);
e = std(y) * ones(size(x));
% 创建向量 x 和 y。绘制 y 对 x 的图。在每个数据点处,显示长度相等的垂直误差条
subplot(1,2,2); errorbar(x , y , e);
t = (1:2:15)' *pi/8; % 等差间隔2Π/8
x = sin(t); % 参数方程表示
y = cos(t);
fill(x,y,'r'); axis square off;
text(0,0,'STOP','Color','w','FontSize',80, ...
'FontWeight','bold','HorizontalAlignment','center');
t = (2:2:8)' *pi/4;
x = sin(t);
y = cos(t);
fill(x,y,'y','LineWidth',5); axis square off;
text(0,0,'WAIT','Color','k','FontSize',65, ...
'FontWeight','bold','HorizontalAlignment','center');
1.[R G B]:0~1之间
G = [46 38 29 24 13];
S = [29 27 17 26 8];
B = [29 23 19 32 7];
name = {
'USA' 'CHN' 'GER' 'FUS' 'KOR'};
h = bar([G' S' B']);
set(h(1),'FaceColor',[1,0.84314,0])
set(h(2),'FaceColor',[0.5,0.54,0.53])
set(h(3),'FaceColor',[198/255,145/255,69/255])
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals');
set(gca,'XTickLabel',name);
xlabel('Country');
legend('Gold','Silver','Bronze');
2.imagesc():
[x , y] = meshgrid(-3 : .2 : 3,-3 : .2 :3);
z = x .^ 2 + x .* y + y .^ 2;
subplot(1,2,1);
surf(x , y , z); box on;
set(gca , 'FontSize',16);
zlabel('z');
xlim([-4 4]); xlabel('x');
ylim([-4 4]); ylabel('y');
subplot(1,2,2);
imagesc(z); axis square; xlabel('x'); ylabel('y');
colorbar; % 显示色阶的颜色栏
x = 0 : 0.1 : 2*pi;
plot(x,sin(x));
【例1】
x = 0 : 0.1 : 3*pi;
z1 = sin(x);
z2 = sin(2*x);
z3 = sin(3*x);
y1 = zeros(size(x));
y3 = ones(size(x));
y2 = y3 ./ 2;
plot3(x,y1,z1,'r',x,y2,z2,'b',x,y3,z3,'g'); grid on;
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
subplot(1,2,1);
t = 0 : pi/50 : 10*pi;
plot3(sin(t) , cos(t) , t);
grid on; axis square;
subplot(1,2,2);
turns = 40 * pi;
t = linspace(0 , turns , 4000);
x = cos(t) .* (turns - t) ./ turns;
y = sin(t) .* (turns - t) ./ turns;
z = t ./ turns;
plot3(x , y , z); grid on;
x = -2 : 1 : 2;
y = -2 : 1 : 2;
[X , Y] = meshgrid(x , y);
Z = X .* Y;
x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X , Y] = meshgrid(x , y);
Z = X .* exp(-X .^ 2 - Y .^ 2);
subplot(1,2,1); mesh(X,Y,Z); title('Mesh');
subplot(1,2,2); surf(X,Y,Z); title('Surf');
x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X , Y] = meshgrid(x , y);
Z = X .* exp(-X .^ 2 - Y .^ 2);
subplot(1,2,1); mesh(X,Y,Z); title('Mesh'); axis square;
subplot(1,2,2); contour(X,Y,Z); title('Contour'); axis square;
x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X , Y] = meshgrid(x , y);
Z = X .* exp(-X .^ 2 - Y .^ 2);
subplot(1,3,1); contour(Z,[- .45 : .05 : .45]); title('Contour1'); axis square;
subplot(1,3,2); [C , h] = contour(Z); clabel(C , h); title('Contour2'); axis square;
subplot(1,3,3); contourf(Z); title('Contour3'); axis square;
x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X , Y] = meshgrid(x , y);
Z = X .* exp(-X .^ 2 - Y .^ 2);
subplot(1,2,1); meshc(X,Y,Z); title('Meshc');
subplot(1,2,2); surfc(X,Y,Z); title('Surfc');
sphere(50); shading flat;
light('Position' , [1 3 2]);
light('Position' , [-3 -1 3]);
material shiny;
axis vis3d off;
set(gcf , 'Color' , [1 1 1]);
view(-45 , 20);
Light(光线):
[X , Y , Z] = sphere(64);
h = surf(X , Y , Z);
axis square vis3d off;
reds = zeros(256 , 3);
reds(: , 1) = (0:256.-1)/255;
colormap(reds); shading interp; lighting phong;
set(h , 'AmbientStrength' , 0.75 , 'DiffuseStrength' , 0.5);
L1 = light('Position' , [-1 , -1 , -1]);
% set(L1 , 'Position' , [-1 , -1 , 1]);
set(L1 , 'Color' , 'g');
v = [0 0 0;1 0 0;1 1 0;0 1 0;0.25 0.25 1;...
0.75 0.25 1;0.75 0.75 1;0.25 0.75 1];
f = [1 2 3 4;5 6 7 8;1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8];
subplot(1,2,1); patch('Vertices' , v , 'Faces' , f , ...
'FaceVertexCData' , hsv(6) , 'FaceColor' , 'flat');
view(3); axis square tight; grid on;
subplot(1,2,2); patch('Vertices' , v , 'Faces' , f , ...
'FaceVertexCData' , hsv(8) , 'FaceColor' , 'interp');
view(3); axis square tight; grid on;
【例】画个山脉地形图
load cape
X = conv2(ones(9,9)/81 , cumsum(cumsum(randn(100 , 100)) , 2));
surf(X , 'EdgeColor' , 'none' , 'EdgeLighting' , 'Phong' , ...
'FaceColor' , 'interp');
colormap(map); caxis([-10 , 300]);
grid off; axis off;