在 MATLAB 中绘制绘图

Hatch a plot in MATLAB

我使用这段代码来生成图表:

而且我必须孵化广场外的所有东西。

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

你想这样做吗?

如果是这样,您应该为轴设置背景颜色

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

并设置矩形的facecolor

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

希望这对您有所帮助。

我做了一个函数hatch_coordinates,它可以return填充图案坐标(获取答案底部的代码)。这样,您只需在轴上绘制填充图案,然后在其上绘制矩形。您必须设置矩形的面颜色以隐藏后面的图案。

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

这会给你:


请注意,阴影线可以多种方式变化。可以通过改变比例 xstep/ystep 来设置角度,并且所有 LineStyle 属性都可用。 一些变体的快速示例:

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


代码:

函数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