matlabmatlab-guide

Hatch a plot in MATLAB


I use this code in order to generate a graph:

And I must to hatch everything outside the square.

pos = [3.75 5.6 53.5 29.5];  %spatiul nemasurat
axis([0 61 0 45]) %axele
set(gca,'YTickLabel',{'0', '7.5','11.85', '16.2', '20.55', '24.9', '29.25', '33.6', '39.3', '45'})

rectangle('Position',pos,'EdgeColor','black')

Figure


Solution

  • I made a function hatch_coordinates which can return hatch pattern coordinates (get the code at the bottom of the anwser). With that, you simply plot your hatch pattern on you axis, then you plot your rectangle on top of it. You have to set the face color of the rectangle to hide the pattern behind.

    xlim = [0 61] ;
    ylim = [0 45] ;
    [X,Y] = hatch_coordinates( xlim , ylim ) ; %// this return coordinates to plot a hatch pattern
    plot(X,Y,'k')                              %// and this simply plot the pattern, with the attributes you want (color, linespec, etc ...)
    hold on ; grid off
    
    pos = [3.75 5.6 53.5 29.5];  %spatiul nemasurat
    axis([0 61 0 45]) %axele
    set(gca,'YTickLabel',{'0', '7.5','11.85', '16.2', '20.55', '24.9', '29.25', '33.6', '39.3', '45'})
    rectangle('Position',pos,'EdgeColor','black','FaceColor','w')
    

    That will give you: hatchedfig


    Note that the hatching can be varied in multiple ways. The angle can be set by changing the ratio xstep/ystep, and all the LineStyle properties are available. Quick example of a few variations:

    xl = [0 5] ; yl = [0 5] ;
    
    %// simple hatch, angle changed
    [X,Y] = hatch_coordinates( xl , yl , 0.2 ) ;
    subplot(1,4,1) ; plot(X,Y) ; grid off
    
    %// heavy line hatching
    [X,Y] = hatch_coordinates( xl , yl , 0.5 ) ;
    subplot(1,4,2) ; plot(X,Y,'k','linewidth',2) ;grid off
    
    %// very light color hatching, flatter angle, dotted lines
    [X,Y] = hatch_coordinates( xl , yl , 1 , 0.1 ) ;
    subplot(1,4,3) ; plot(X,Y,'Color',[.7 .7 .7],'linewidth',1,'LineStyle',':') ;grid off
    
    %// multi color hatching, (specify option "merge=false" )
    [X,Y] = hatch_coordinates( xl , yl , 0.5 , 0.5 , false ) ;
    subplot(1,4,4) ; plot(X,Y) ;grid off
    

    multihatch


    Code:

    function hatch_coordinates.m :

    function [X,Y] = hatch_coordinates( xlim , ylim , xstep , ystep , merge )
    %// function [X,Y] = hatch_coordinates( xlim , ylim , xstep , ystep , merge )
    %//
    %// Return coordinates for plotting a hatch pattern
    %// The angle of the lines can be adjusted by varying the ratio xstep/ystep
    %// xlim and ylim are vectors with two elements, where the first element needs to be smaller than the second.
    
    %% // set default options
    if nargin < 3 ; xstep = 1     ; end
    if nargin < 4 ; ystep = xstep ; end
    if nargin < 5 ; merge = true  ; end
    
    %% // define base grid
    xpos = xlim(1):xstep:xlim(2) ; nx = numel(xpos) ;
    ypos = ylim(1):ystep:ylim(2) ; ny = numel(ypos) ;
    
    %% // Create the coordinates
    nanline = NaN*ones(1,nx+ny-3) ;
    X = [ [ xpos(1)*ones(1,ny-2) xpos(1:end-1) ] ; ...
          [ xpos(2:end) xpos(end)*ones(1,ny-2) ] ; ...
          nanline ] ;
    Y = [ [ypos(end-1:-1:1) ylim(1)*ones(1,nx-2) ]  ; ...
          [ypos(end)*ones(1,nx-1) ypos(end-1:-1:2)] ; ...
          nanline ] ;
    
    %% // merge if asked too
    if merge
        X = X(:) ;
        Y = Y(:) ;
    end