function plotmode(om, alp, shapes, x, y, tri, im)

if nargin < 7
im = [];
end

nMode = size(shapes,2);
shapes = bsxfun(@times, shapes, 1./max(abs(shapes), [], 1));
shapes(end+1:length(x),:) = 0;
[om, q] = sort(om);
alp = alp(q);
shapes = shapes(:, q);

MainFigure = figure;

b(1) = uicontrol(MainFigure, 'Style', 'PushButton', 'String', 'Next', ...
'Callback', @(src,event)NextCallback(src,1,nMode));
b(2) = uicontrol(MainFigure, 'Style', 'PushButton', 'String', 'Prev', ...
'Callback', @(src,event)NextCallback(src,-1,nMode));
b(3) = uicontrol(MainFigure, 'Style', 'PushButton', 'String', 'Stop', ...
'Callback', @(src,event)StopCallback(src));
AlignItems(b, 'left');
AlignItems(b, 'row up', 10);

if ~isempty(im)
image(im);
end
hold on;
p0 = patch(x(tri'), y(tri'), zeros(size(tri')), zeros(size(tri')));
set(p0, 'FaceColor', 'none', 'EdgeColor', 'blue');
p = patch(x(tri'), y(tri'), zeros(size(tri')), zeros(size(tri')));
if ~isempty(im)
alpha .7;
shading interp;
end
caxis([-1 1]);
set(gca, 'ZLim', [-1 1]);
axis equal tight;

iMode = 1;
data.iMode = iMode;
data.isRunning = true;
guidata(MainFigure, data);

nPhi = 20;
phivec = (0:nPhi-1)/nPhi*2*pi;
iPhi = 1;
while data.isRunning
iPhi = mod(iPhi-1, nPhi)+1;
data = guidata(MainFigure);
title(sprintf('f_%d = %g Hz', data.iMode, om(data.iMode)/2/pi));
q = shapes(:,data.iMode) * exp(1i*phivec(iPhi));
set(p, 'CData', real(q(tri')));
set(p, 'ZData', real(q(tri')));
set(gca, 'ZLim', [-1 1]);
iPhi = iPhi+1;
pause(.1);
end
end

function NextCallback(src,inc,lim)
data = guidata(src);
data.iMode = mod(data.iMode+inc-1,lim)+1;
guidata(src,data);
end

function StopCallback(src)
data = guidata(src);
data.isRunning = false;
guidata(src,data);
end