Dynamic colorbar change with window size corresponding with different data area

조회 수: 11 (최근 30일)
Hey guys~
When we zoom in or zoom out the figure, I wonder how to generate a colorbar dynamically show the current area, that is, a colorbar changing with current window.
you see, when i zoomed in the figure, the value of the colorbar did not change to show the cunrrent elevation of the area.
Thanks a lot!

채택된 답변

Chunru
Chunru 2021년 9월 9일
You can use the callback function of zoom to customize what you want.
z = peaks(200);
hi = imagesc(1:200, 1:200, z);
colorbar
h = zoom();
h.ActionPostCallback = @changecolorbar;
function changecolorbar(src, ~)
h = gco;
xl = xlim;
yl = ylim;
ix = find(h.XData>=xl(1) & h.XData<=xl(2));
iy = find(h.YData>=yl(1) & h.YData<=yl(2));
C = h.CData(ix,iy);
caxis([min(C(:)) max(C(:))]);
end
  댓글 수: 3
Chunru
Chunru 2021년 9월 9일
편집: Chunru 2021년 9월 9일
For dragging, you need to have the different callback function. The code above is just for zoom callback. You can do the similar by setting the pan callback.
z = peaks(200);
hi = imagesc(1:200, 1:200, z);
colorbar
h = zoom();
hpan = pan(gcf);
h.ActionPostCallback = @changecolorbar;
hpan.ActionPostCallback = @changecolorbar;
function changecolorbar(src, ~)
h = gco;
xl = xlim;
yl = ylim;
ix = find(h.XData>=xl(1) & h.XData<=xl(2));
iy = find(h.YData>=yl(1) & h.YData<=yl(2));
C = h.CData(ix,iy);
caxis([min(C(:)) max(C(:))]);
end
Meillo Fang
Meillo Fang 2021년 9월 9일
WOW! It worked, the only thing left here is that, when i zoom in or zoom out the figure, the color of the figure changes but the color of the colorbar doesn't change, how can i make the color of the whole figure stay but the color of the colorbar change when the selected area of the figure changes.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by