• Perfect subplot in Matlab


    http://p-martineau.com/perfect-subplot-in-matlab/

    In my field, it is often very useful to include many panels in one single figure. Matlab offers by default the subplot function but it is not easily customizable and often leaves me unsatisfied. For high quality publishable figures, I wrote the function subplot_pos :

    function [ positions ] = subplot_pos(plotwidth,plotheight,leftmargin,rightmargin,bottommargin,topmargin,nbx,nby,spacex,spacey)
     
        subxsize=(plotwidth-leftmargin-rightmargin-spacex*(nbx-1.0))/nbx;
        subysize=(plotheight-topmargin-bottommargin-spacey*(nby-1.0))/nby;
     
        for i=1:nbx
           for j=1:nby
     
               xfirst=leftmargin+(i-1.0)*(subxsize+spacex);
               yfirst=bottommargin+(j-1.0)*(subysize+spacey);
     
               positions{i,j}=[xfirst/plotwidth yfirst/plotheight subxsize/plotwidth subysize/plotheight];
     
           end
        end
    end
    

      

    This function takes the size of the panels and the figure in centimeters as parameters and outputs a cell containing axes positions. The resulting figure has the exact specified dimensions. The following code shows how to use this Matlab sub-plotting function :

    %parameters for figure and panel size
    plotheight=20;
    plotwidth=16;
    subplotsx=3;
    subplotsy=5;   
    leftedge=1.2;
    rightedge=0.4;   
    topedge=1;
    bottomedge=1.5;
    spacex=0.2;
    spacey=0.2;
    fontsize=5;    
    sub_pos=subplot_pos(plotwidth,plotheight,leftedge,rightedge,bottomedge,topedge,subplotsx,subplotsy,spacex,spacey);
     
    %setting the Matlab figure
    f=figure('visible','on')
    clf(f);
    set(gcf, 'PaperUnits', 'centimeters');
    set(gcf, 'PaperSize', [plotwidth plotheight]);
    set(gcf, 'PaperPositionMode', 'manual');
    set(gcf, 'PaperPosition', [0 0 plotwidth plotheight]);
     
    %loop to create axes
    for i=1:subplotsx
    for ii=1:subplotsy
     
    ax=axes('position',sub_pos{i,ii},'XGrid','off','XMinorGrid','off','FontSize',fontsize,'Box','on','Layer','top');
     
    z=peaks;
    contour(z);
     
    if ii==subplotsy
    title(['Title (',num2str(i),')'])
    end
     
    if ii>1
    set(ax,'xticklabel',[])
    end
     
    if i>1
    set(ax,'yticklabel',[])
    end
     
    if i==1
    ylabel(['Ylabel (',num2str(ii),')'])
    end
     
    if ii==1
    xlabel(['Ylabel (',num2str(i),')'])
    end
     
    end
    end
     
    %Saving eps with matlab and then producing pdf and png with system commands
    filename=['test'];
    print(gcf, '-depsc2','-loose',[filename,'.eps']);
    system(['epstopdf ',filename,'.eps'])
    system(['convert -density 300 ',filename,'.eps ',filename,'.png'])
    

      

    And the resulting figure looks like :

    Perfect subplot with Matlab

    Custom subplot function for Matlab. Publishable quality figures.

    If you display the figure at 100% of its size and measure it with a ruler, you will see that the figure respects perfectly the dimensions specified in the code. This code is fully customizable and can be modified to suit your needs. Anything is possible… Much better than the default Matlab subplot function!

     
  • 相关阅读:
    他扎根农村种植双孢菇,从门外汉成为了“土专家”
    农村小伙成立生态农业基地,带领村民打开致富新门路
    小伙让蜜柚成为真正的“黄金柚”,帮助600多农户发家致富
    js时间格式化函数,支持Unix时间戳
    js时间格式化函数,支持Unix时间戳
    js时间格式化函数,支持Unix时间戳
    js时间格式化函数,支持Unix时间戳
    Netty随记之ChannelInboundHandlerAdapter、SimpleChannelInboundHandler
    Netty随记之ChannelInboundHandlerAdapter、SimpleChannelInboundHandler
    Netty随记之ChannelInboundHandlerAdapter、SimpleChannelInboundHandler
  • 原文地址:https://www.cnblogs.com/gisalameda/p/8444959.html
Copyright © 2020-2023  润新知