D-P

博客园 首页 新随笔 联系 订阅 管理

第五章:初级绘图进阶

Special Plots

 loglog
 semilogx
 semilogy
 plotyy
 hist
 bar
 pie
 polar

 

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');    %给最后一个图的XGrid加坐标线
 set(gca,'YGrid','on');

 

Y & Y

 #在2020B版本中加入yyaxis函数创建具有两个YGrid的图
 plotyy()   %显示两个YGrid
 EG(阻尼简谐运动):
 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);    %AX代表axis,为坐标轴;将y1的权柄赋予H1
 set(get(AX(1),'Ylabel'),'String','Left Y-axis')   %AX(1)代指左边y轴
 set(get(AX(2),'Ylabel'),'String','Reft Y-axis')   %AX(2)代指右边y轴
 title('Labeling plotyy');
 set(H1,'LineStyle','--');
 set(H2,'LineStyle',':');

 

Histogram

 y = randn(1,1000);    %按正态分布的随机数
 subplot(2,1,1);
 title('Bins = 10');
 subplot(2,1,2);
 hist(y,50);
 title('Bins = 50');

 

Bar Charts

 x = [1 2 5 4 8];    
 y = [x;1:5];        %不同的组---[1 2 5 4 8];[1 2 3 4 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');

Stacked(堆叠式) and Horizontal(水平的)

 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);     %h = horizontal    
 %barh(y,'stacked') 水平堆叠式
 title('Horizontal');

 

Pie Chart

 a = [10 5 20 30];
 subplot(1,3,1);
 pie(a);
 subplot(1,3,2);
 pie(a,[0,0,0,1]);
 subplot(1,3,3);
 pie3(a,[0,0,0,1])
 %更多关于pie自查

 

Polar Chart(极坐标)

 x = 1:100;
 theta = x/10;
 r = log10(x);
 subplot(1,4,1);
 polar(theta,r);
 theta = linspace(0,2*pi);
 r = cos(4*theta);
 subplot(1,4,2);
 polar(theta,r);
 theta = linspace(0,2*pi,6);      %第六个点和第一个点重合
 r = ones(1,length(theta));
 subplot(1,4,3);
 polar(theta,r);
 theta = linspace(0,2*pi);
 r = 1-sin(theta);
 subplot(1,4,4);
 polar(theta,r);
 %自查意义(XP懒

 

Stairs and Stem Charts

x = linspace(0,4*pi,40);
y = sin(x);
subplot(1,2,1);
stairs(y);   %阶梯图
subplot(1,2,2);
stem(y);     %绘制离散序列数据

 

Boxplot and Error Bar

load carsmall
boxplot(MPG,Origin);
x = 0:pi/10:pi;
y = sin(x);
e = std(y)*ones(size(x));
errorbar(x,y,e);   %e为误差条的长度

 

fill

fill()
eg:
t = (1:2:15)'*pi/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');
	%自行查询Text属性
EG:
t = (1:4)'*pi/2;
x = sin(t);
y = cos(t);
fill(x,y,'y');
axis square off;
text(0,0,'WAIT','Color','k','FontSize',60,...
	'FontWeight','bold','HorizontalAlignment','center','LineWidth',1.5);

 

Color Space

[R G B];
%0 is minimum;1 is maximum;
White:FFFFFF=[255 255 255]    %十六进制转换

Exercise:更换bar的一个条子的颜色{可自查Bar属性/Set}

G = [46 38 29 24 13];
S = [29 27 17 26 8];
B = [29 23 19 32 7];
h = bar(1:5,[G' S' B']);    %按列分组1:5;
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals');
xlabel('Country');
legend('Gold','Silver','Bronze');
set(gca,'XTickLabel',...
	{'USA','CHN','GBR','RUS','KOR'});
set(h(1),'facecolor',[1 0.8 0.2]);  
set(h(2),'facecolor',[0.8 0.8 0.8]);  
set(h(3),'facecolor',[0.8 0.4 0]);  

 

imagesc()

[x, y] = meshgrid(-3:.2:3,-3:.2:3);
z = x.^2+x.*y+y.^2;
surf(x,y,z);
box on;
set(gca,'FontSize',16);
zlabel('z');
xlim([-4 4]);
xlabel('x');
ylim([-4 4]);
ylabel('y');
%%%
imagesc(z);
axis square;
xlabel('x');
ylabel('y');

colorbar&colormap([Name])

colorbar;   %颜色变化
colormap([Name]);
%颜色变化
%可以看做:
a = ones(256,3);
colormap(a)
%自查colormap

 

3D Plots

plot3
surf
surfc
surface
meshc
contour
contourf

 

plot3()

EG1:
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');
EG2:
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
grid on;
axis square;
EG3:
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;

 

Surface

meshgrid()   %组成网格
eg:
x = -2:1:2;
y = -2:1:2;
[X,Y] = meshgrid(x,y);
mesh()  &  surf()
eg:
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);
subplot(1,2,2);
surf(X,Y,Z);
contour()
eg:
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(2,1,1);
mesh(X,Y,Z);
axis square;
subplot(2,1,2);
contour(X,Y,Z);
axis square;
contour(Z,[-.45:.05:.45])
clabel()
contourf()  %fill
eg:
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])
axis square;
subplot(1,3,2);
[C,h] = contour(Z);
clabel(C,h);
axis square;
subplot(1,3,3);
contourf(Z);
axis square;

 

meshc() & surfc()

eg:
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);
subplot(1,2,2);
surfc(X,Y,Z);

 

view() & light()

eg:
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);
%调整角度
eg:
[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');

 

patch()

eg:
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;

A professional graph

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;
%random graph,很好玩(赞)

 

posted on 2021-04-03 15:14  D-P  阅读(178)  评论(0编辑  收藏  举报