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!