Hatch a plot in MATLAB

淺唱寂寞╮ 提交于 2019-12-22 01:04:15

问题


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


回答1:


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:


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


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

%% // 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) zeros(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



回答2:


Is that you want to do?

if so, you should set a background color for the axis

set(gca,'color','r')

and set the facecolor of the rectangle

r=rectangle('Position',pos,'EdgeColor','black','faceColor','black')

Hope this helps.



来源:https://stackoverflow.com/questions/29949205/hatch-a-plot-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!