Matlab-进阶绘图

04进阶绘图

Advanced 2D plots

Logarithm Plots 对数绘图

  • x = logspace(-1,1,100) 即x从10^-1 到 10^1 一百个项

  • x = linspace(0,4*pi,40) 即 x 从0 到 4pi 40个项

  • loglog (x,y) 对两个轴取对数 log 以10为底

  • semilogx (x,y) 对x轴取对数

  • semilogy (x,y) 对y轴取对数

    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’);

  • plotyy(x,y1,x,y2) x相同,y函数不同的对比,左右两个y轴

    x = 0:0.01:20;
    y1 = 200exp(-0.05x).sin(x);
    y2 = 0.8
    exp(-0.5*x).sin(10x);
    [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’,’:

    统计图表 柱状图 饼状图 极坐标图 梯状图

  • hist (变量,几个柱) 整体分布情况

    y = randn(1,1000);
    subplot(2,1,1);
    hist(y,10);
    title(‘Bins = 10’);
    subplot(2,1,2);
    hist(y,50);
    title(‘Bins = 50’);

  • bar bar charts 单自变量的统计图

    • bar(变量,‘stacked’) -->堆高起来的单个变量
    • barh(变量) -->柱高向右的柱状图

    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’);

  • pie 饼状图

    • pie(变量,[0,0,1]) 对于变量的数组中的每个元素是否分裂开,1true,2false
    • pie3 以3D形式绘图

    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]);

  • polar 极坐标图

    • polar(theta,r) 参数为theta,极坐标平均分成几份 ,r=f(x) ,r半径为x的函数

    x = linspace(0, 4pi, 40); y = sin(x);
    subplot(1,2,1); stairs(y);
    subplot(1,2,2); stem(y);
    x = 1:100; theta = x/10; r = log10(x);
    subplot(1,4,1); polar(theta,r);
    theta = linspace(0, 2
    pi); r = cos(4theta);
    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);

  • stairs 梯状图

  • stem 线状图

    x = linspace(0, 4*pi, 40); y = sin(x);
    subplot(1,2,1); stairs(y);
    subplot(1,2,2); stem(y);

  • boxplot

    load carsmall
    boxplot(MPG, Origin);

  • error bar e是bar的长度,统计论的误差值,自相关函数

    x=0:pi/10:pi; y=sin(x);
    e=std(y)*ones(size(x));
    errorbar(x,y,e)

Stop sign Wait sign绘制符号

  • fill

  • 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’);

    t=(0:4)’*pi/2;x=sin(t);y=cos(t);
    fill(x,y,‘y’,‘LineWidth’,5.0);
    axis square off;
    text(0,0,‘WAIT’,‘color’,‘k’,‘FontSize’,70,‘FontWeight’,‘bold’,…
    ‘HorizontalAlignment’,‘center’)

Color space

  • [R G B]

  • 0 is minimum 1 is maximum

  • 8-bit equivalence: 0 is minimum 255 is maximum

  • 16-bit equivalence: 00 is minimum FF is maximum

  • 改变颜色 绘制五个国家的的金银铜牌数

    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’]);

    title(‘Medal count for top 5 countries in 2012 Olympics’);

    ylabel(‘Number of medals’); xlabel(‘Country’);

    legend(‘Gold’, ‘Silver’, ‘Bronze’)

  • 色彩作为另一个维度

  • meshgrid() 生成3D网格采样点

  • 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;指令 显示2D的图的色彩作为第3D的值

  • colormap(预设色图name)

    • 常用name:jet,hsv,hot,coolspring,summer,autumn,winter
    • 不常用name:parula,gray,bone,copper,pink,lines,colorcube,prism

3D plots

  • plot3 基本3D画图

    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’);

    t = 0:pi/50:10*pi;

    plot3(sin(t),cos(t),t)

    grid on; axis square;

    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;

  • meshgrid() 生成3D网格采样点

    • 利用mashgird(x,y)生成一个矩阵 x是一个级数,y是一个级数
    • x = -2:1:2; y = -2:1:2;
    • [X,Y] = meshgrid(x,y)
  • surf 实色填充3d网格 mesh仅有网格 画图

    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);

  • surfc meshc :同时在底部加上等高线画图

  • surface

  • contour(X,Y,Z) 等高线图

    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;

  • contourf(Z) 实色填充等高线图 Clabel(C,h) 标记等高线数值

    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;

  • light()打光角度

  • 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);

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;

你可能感兴趣的:(Matlab,Matlab)