Changing width of color bar and re-positioning it in movie frames
5 次查看(过去 30 天)
显示 更早的评论
Hello. I have data I'm using to make a movie from surface plots. I'm trying to add a fixed color bar with a width about 1/2 times the default, but adjusting the width causes the color bar to overlap each plot. My code only ends up correcting this for the first frame, but does not correct it for the rest of them. Any suggestions would be greatly appreciated.
set(gcf,'visible','off')
mov(pf) = struct('cdata',[],'colormap',[]);
for p = 1:pf
surf(X(:,:,p),Y(:,:,p),P(:,:,p));
grid('off')
xlabel('x(m)');
ylabel('y(m)');
zlabel('|Grad(P)| (Pa/m)');
axis([x0 xf y0 yf z0 zf])
titleInfo = cell(3,1);
titleInfo{1,1} = '|GradP| vs x and y';
titleInfo{2,1} = ['U = ',num2str(u,'%3.2f'),' (m/s)'];
titleInfo{3,1} = ['t = ',num2str(t(p),'%3.2f'),' (s)'];
title(titleInfo);
view(50,45)
colormap jet
caxis manual
caxis([z0 zf])
*cbar = colorbar;
ax = gca;
axpos = ax.Position;
cpos = cbar.Position;
cpos(3) = 0.5*cpos(3);
cbar.Position = cpos;
ax.Position = axpos;*
mov(p) = getframe(gcf);
end
close('all')
set(gcf,'visible','on')
0 个评论
采纳的回答
Mike Garrity
2016-3-28
I think that the colorbar and axes positions aren't really stable at the point that you're changing the Position. Possibly because the figure is Visible='off', but I'm not sure about that. You can probably get things to be a bit more stable if you insert a drawnow after call the colorbar.
But, when I'm doing an animation like this, I usually pull the plotting function out of the loop and use the returned handle to update the data. The reason for this is that then all of the axes positioning is only happening once, not every frame. That tends to make things a lot more stable.
For your example, that might look something like this:
set(gcf,'visible','off')
mov(pf) = struct('cdata',[],'colormap',[]);
% Initialization
h = surf(X(:,:,1),Y(:,:,1),P(:,:,1));
grid('off')
xlabel('x(m)');
ylabel('y(m)');
zlabel('|Grad(P)| (Pa/m)');
axis([x0 xf y0 yf z0 zf])
view(50,45)
colormap jet
caxis manual
caxis([z0 zf])
cbar = colorbar;
ax = gca;
axpos = ax.Position;
cpos = cbar.Position;
cpos(3) = 0.5*cpos(3);
cbar.Position = cpos;
ax.Position = axpos;
% Animation
for p = 1:pf
h.XData = X(:,:,p);
h.YData = Y(:,:,p);
h.ZData = P(:,:,;);
titleInfo = cell(3,1);
titleInfo{1,1} = '|GradP| vs x and y';
titleInfo{2,1} = ['U = ',num2str(u,'%3.2f'),' (m/s)'];
titleInfo{3,1} = ['t = ',num2str(t(p),'%3.2f'),' (s)'];
title(titleInfo);
mov(p) = getframe(gcf);
end
close('all')
set(gcf,'visible','on')
4 个评论
Mike Garrity
2016-3-28
Really? If you run it with the figure visible, can you see the title changing?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Color and Styling 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!