在MATLAB中绘制图 [英] Hatch a plot in MATLAB

查看:106
本文介绍了在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')

推荐答案

我制作了一个函数hatch_coordinates,该函数可以返回填充图案坐标(在代码框的底部获取代码).这样,您只需在轴上绘制填充图案,然后在其顶部绘制矩形即可.您必须设置矩形的表面颜色才能将图案隐藏在后面.

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

这将为您提供:

请注意,阴影可以多种方式变化.可以通过更改比率xstep/ystep来设置角度,并且所有LineStyle属性均可用. 几个变体的简单示例:

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

功能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

这篇关于在MATLAB中绘制图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆