Scale patch objects relative to plot Zoom level
Show older comments
I have a function to plot wind speed with directional arrows overlayed on top.
Each arrow is created using a patch object.
To make the arrows a nice size to see, the patch is scaled relative to the current window and axis size.
It works great the first time you plot it, the problem is that when you zoom in on the graph the patches are no longer the correct scale and get distorted.
Is there a way I can get them to recalculate the scaling factor after each zoom and update?
Can I use a different coordinate system for the patches relative to the axis size?
Alternative to patches?
time = datetime("today")-14:datetime("today");
speed = rand(size(time));
dir = linspace(0,360,length(time));
figure
plot_wind(time,speed,dir);
figure
plot_wind(time,speed,dir);
zoom xon
zoom(5)
function h = plot_wind(varargin)
if size(varargin,2)==2
Time=1:length(varargin{1});
WindSpd=varargin{1};
WindDir=varargin{2};
h=axes;
Skip=1;
hl = plot(WindSpd);
elseif size(varargin,2)==3
Time=varargin{1};
WindSpd=varargin{2};
WindDir=varargin{3};
Skip=1;
h=axes;
hl = plot(Time,WindSpd);
elseif size(varargin,2)==4
h=varargin{1};
Time=varargin{2};
WindSpd=varargin{3};
WindDir=varargin{4};
Skip=1;
hl = plot(h,Time,WindSpd);
elseif size(varargin,2)==5
h=varargin{1};
Time=varargin{2};
WindSpd=varargin{3};
WindDir=varargin{4};
Skip=varargin{5};
hl = plot(h,Time,WindSpd);
end
set(hl,'ZData', zeros(size(Time)));
WindowSize=get(gcf,'Position');%[left bottom width height]
AxesSize=get(h,'Position');%[left bottom width height]
xscalefactor=1.5*diff(get(h,'xlim'))*0.03*WindowSize(4)/WindowSize(3)*AxesSize(4)/AxesSize(3);
yscalefactor=1.5*diff(get(h,'ylim'))*0.03;
[theta,rho]=cart2pol([1,0.5,0.5,-1,-1,0.5,0.5],[0,0.5,0.25,0.25,-0.25,-0.25,-0.5]);% Create the shape of the arrow and express in polar coordinates
for p=1:Skip:length(WindDir)
[u,v]=pol2cart(theta-pi/2+WindDir(p).* pi ./ 180,rho);% Add the rotation angle to the arrow and convert back to cartesian coordinates
c=hsv(361);
hp = patch(-u.*xscalefactor+Time(p),v.*yscalefactor+WindSpd(p),c(int16(WindDir(p)+1),:));% create the arrow patch scaled to size based on current window size
set(hp, 'ZData', ones(size(u))*p);
end
end
1 Comment
Robert Daly
on 1 Dec 2023
Accepted Answer
More Answers (0)
Categories
Find more on Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


