Dynamic colorbar change with window size corresponding with different data area
15 次查看(过去 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/733334/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/733339/image.png)
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!
0 个评论
采纳的回答
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
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!