Create colorbar outside of parent of axes

12 ビュー (過去 30 日間)
Rolf
Rolf 2023 年 4 月 3 日
Hi, I have an axes displaying image data inside a panel, which is the parent of this axes.
Now I would like to create a colorbar for this image data outside of the panel which surrounds the axes. Is there any way to link the colorbar not directly to the axes but to the panel, so that
set(myColorbar, 'Location', 'eastoutside')
will result on my colorbar on the right of the panel (instead of on the right of the axes, but inside the panel, as it is behaving now)?
I would be interested in general approach, where the data for the content of the colorbar (e.g. the colors itself) would maintain associated with the axes to reflect later changes and the position & size of the colorbar will be associated to the panel.
Thanks in advance for any help

回答 (1 件)

Constantino Carlos Reyes-Aldasoro
Constantino Carlos Reyes-Aldasoro 2023 年 4 月 4 日
That is easy, when you create figures and plots, you can grab the handle and then you can modify its properties:
a = rand(64);
imagesc(a)
h1=colorbar;
We have now grabbed the handle of the colorbar, and one property is the position:
h1.Position
ans = 1×4
0.8284 0.1109 0.0381 0.8152
Now, you only need to set the position to whatever you need:
h1.Position=[0.4 -0.2 0.05 0.6];
  2 件のコメント
Rolf
Rolf 2023 年 4 月 5 日
unfortunaly this does not work in my case, since the Axes is contained in a Panel and setting the position with negativ values outside of the panel lets it disappear.
Constantino Carlos Reyes-Aldasoro
Constantino Carlos Reyes-Aldasoro 2023 年 4 月 5 日
I am not sure if we can simulate the panels here, but I can move the colorbar of a second plot to be on top of the first plot:
h0=gcf;
h1=subplot(121);
imagesc(rand(64));
hc1=colorbar;
h2=subplot(122);
imagesc(100*rand(16));
hc2=colorbar;
hc2.Position=[0.12 0.1 0.02 0.8];
We can also modify the position of the individual axes:
h1.Position =[0.2 0.3 0.2 0.3];

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRed についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by