这里写目录标题
二维绘图
对数图
semilogx()
semilogy()
loglog()
semilogx(x,y);//x轴是对数坐标
semilogy(x,y);
loglog(x,y);
两个y轴
plotyy()
[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');
set(H1,'LineStyle','--');
set(H2,'LineStyle',':');
直方图
hist()
y=randn(1,1000);
hist(y,50);//Bins=50
条形图
bar()
bar3()
barh()
x1=[1,2,5,4,8];
x2=[...];
x3=[...];
X=[x1;x2;x3]
bar(x1);//无组别的条形图
bar(X);//有三个组
bar3(X);//3D
bar(X,'stacked');//堆在一起的条形图
barh(X);//horizontal,横过来的
饼图
pie()
pie3()
a=[10,5,20,30];
pie(a);
pie(a,[0,0,0,1]);//1对应的扇形被分隔出来
pie3(...);//3D
雷达图
polar()
theta=linspace(0,2*pi);
r=1-sin(theta);
polar(theta,r);
阶梯图
stairs()
x=linspace(0,4*pi,40);
y=sin(x);
stairs(x,y);
茎状图
stem()
x=linspace(0,4*pi,40);
y=sin(x);
stem(x,y);
箱形图
boxplot()
boxplot(x,y);
误差条
errorbar()
x=0:pi/10:pi;
y=sin(x);
err=std(y)*ones(size(x));//误差
errorbar(x,y,e);
填充颜色
fill()
theta=(1:2:15)'*pi/8;
x=sin(t);
y=cos(t);
fill(x,y,'r');//多边形内部填充红色
text(0,0,'STOP','Color','w','FontSize',80,'FontWeight','bold','HorizontalAlignment','center');
颜色空间
[R,G,B]三个通道,每个取值[0,1],0最浅,1最深。对应8位颜色的[0,255]。
将矩阵显示成图像
imagesc()
[x,y]=meshgrid(-3:0.2:3,-3:0.2:3);
z=x.^2+x.*y+y.^2;
imagesc(z);
colorbar;//颜色图例
colormap(hot);//采用什么配色方案
colormaps
colormap实际上是一个256*3的矩阵,例如:
myMap=ones(256,3);
colormap(myMap);
内置colormaps包括:
三维绘图
3D图线
plot3()
plot3(x1,y1,z1,'r',x2,y2,z2,'g',...);
例子:
t=0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
grid on;
axis square;
3D表面
mesh()
网格表面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);
subplot(1,2,2);
surf(X,Y,Z);
等高线图
contour()
contourf()
填充颜色
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);
contour(X,Y,Z);
或者
contour(Z,[-0.45:0.05:0.45]);//密集程度
或者
[C,h]=contour(Z);
clabel(C,h);//有数字
或者
contourf(Z);
3D表面+等高线投影
meshc()
surfc()
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);
meshc(X,Y,Z);
或者
surfc(X,Y,Z);
改变视角
view()
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');//改变光的颜色
绘制多面体
patch()
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;