Hatch a plot in MATLAB -
i use code in order generate graph:
and must hatch outside 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')
i made function hatch_coordinates
can return hatch pattern coordinates (get code @ bottom of anwser). that, plot hatch pattern on axis, plot rectangle on top of it. have set face color of rectangle hide pattern behind.
xlim = [0 61] ; ylim = [0 45] ; [x,y] = hatch_coordinates( xlim , ylim ) ; %// return coordinates plot hatch pattern plot(x,y,'k') %// , plot pattern, attributes 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 give you:
note hatching can varied in multiple ways. angle can set changing ratio xstep
/ystep
, , linestyle
properties available. quick example of 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 %// 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
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 plotting hatch pattern %// angle of lines can adjusted varying ratio xstep/ystep %% // 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 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) zeros(1,nx-2)] ; ... [ypos(end)*ones(1,nx-1) ypos(end-1:-1:2)] ; ... nanline ] ; %% // merge if asked if merge x = x(:) ; y = y(:) ; end
Comments
Post a Comment