How do I move the colorbar outside of an image and still maintain a fixed height?
8 views (last 30 days)
Show older comments
MathWorks Support Team
on 7 Sep 2021
Edited: MathWorks Support Team
on 31 Jan 2025
I am trying to load images of different size ratios into an UIAxes while keeping the colorbar outside of the image and maintaining a fixed height. When I change the position of the colorbar, the image changes its size and overlays with the "colorbar".
Accepted Answer
MathWorks Support Team
on 17 Jan 2025
Edited: MathWorks Support Team
on 31 Jan 2025
The issue is that once the "Position" property of the colorbar is manually set, the axes start to ignore the colorbar and will no longer automatically resize to accommodate the colorbar. Unfortunately, the "InnerPosition" property of UIAxes cannot be manually changed prior to R2020b.
Below are two workarounds. Both of them require using regular axes instead of UIAxes:
1. Use regular axes instead of UIAxes and manually adjust the position of axes and colorbar.
Unlike the "InnerPosition" property of UIAxes, we can set the "Position" property of regular axes. In this workaround, we manually set the value of the "Position" property of the colorbar and regular axes. The example code is as below:
%create the figure fig = uifigure; %create the regular axes , set the NextPlot to replacechildren ax = axes(fig, 'NextPlot', 'replacechildren'); %load image, we use peppers.png as an example img = imshow('peppers.png', 'parent', ax); %create colorbar cb = colorbar(ax); %manually change the position of colorbar to what you want cb.Position = [0.8838 0.1600 0.0400 0.7000] %manually change the position of the image by changing the position of the regular axes ax.Position = [0.1255 0.1349 0.6 0.8050];
After modifying the Position values of axes and colorbar, the value will remain the same when you load in a new image.
2. Use regular axes instead of UIAxes and put the axes into a tile chart layout
This workaround is to put the axes into a "tiledlayout", which creates a tiled chart layout to display plots in the current figure. This will automatically keep the colorbar outside of the image with a fixed position and automatically adjust the size of the image. The example code is as below:
%create the figure fig = uifigure; %set the tiled layout t = tiledlayout(fig,'flow'); %create the regular axes ax = axes(t, 'NextPlot', 'replacechildren'); %create colorbar cb = colorbar(ax); %load your image, we use peppers.png as an example img = imshow('peppers.png', 'parent', ax);
For more information about the "NextPlot" property of "axes", please execute the following command in the MATLAB R2019b command window to access the release-specific documentation:
>> web(fullfile(docroot, 'matlab/ref/matlab.graphics.axis.axes-properties.html'))
Please follow the below link to search for the required information regarding the current release:
0 Comments
More Answers (0)
See Also
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!