Surf and XLIM YLIM -- setting the limits on a 3D graph doesnt seem to work?

18 ビュー (過去 30 日間)
Matlab2010
Matlab2010 2013 年 2 月 14 日
I have the following code
y=randn(50,50);
x=randn(50,50);
z=randn(50,50);
surf(y,x,z);
set(gca,'xlim',[0 2],'ylim',[0 2]);
Hence I only wish to show the surface between 0 and 2 on the x and y axis.
however, xlim ylim do not seem to work as per normal here. The data is shown overhanging the axis and doesnt look neat and tidy.
what have i done wrong? thank you

採用された回答

Sean de Wolski
Sean de Wolski 2013 年 2 月 14 日
編集済み: Sean de Wolski 2013 年 2 月 14 日
Clipping is known to not necessarily work for three-d plots:
With newer versions of MATLAB you can use addlistener to listen to the 'PostSet' limit events on the axes and update the surface's data with new data NaNing out everything outside of the new limits.
Alright, you piqued my interest:
function Clip3d
%Clip a three dimensional surface example
%
% Sean de Wolski 02/14/2013 - MathWorks
%
%Data
yy=randn(50,50);
xx=randn(50,50);
zz=randn(50,50);
%Objects
figure;
ax = axes;
h = surf(xx,yy,zz);
drawnow;
xlimits = get(ax,'XLim');
ylimits = get(ax,'YLim');
%Listen to changes
addlistener(ax,'XLim','PostSet',@(src,evt)ClipMe(src,evt));
addlistener(ax,'YLim','PostSet',@(src,evt)ClipMe(src,evt));
function ClipMe(src,evt)
%When limits change
zzNaN = zz; %copy
%Which changed?
if strcmp(get(src,'Name'),'XLim')
xlimits = evt.NewValue;
else
ylimits = evt.NewValue;
end
%Clip with Nans
zzNaN(xx<xlimits(1) | xx>xlimits(2)) = NaN;
zzNaN(yy<ylimits(1) | yy>ylimits(2)) = NaN;
set(h,'ZData',zzNaN); %update
drawnow;
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by