Control location of exponent on a colorbar

27 views (last 30 days)
Hello All,
I am making a figure that is a 3x2 grid of surface plots with colorbars on each. I noticed sometimes when I make this figure the exponent on the colorbar is palced on the bottom instead of the top, is there a way to set it to always be at the top?
I found a similar post from 2016, but it appears there was no way to do this then (https://www.mathworks.com/matlabcentral/answers/278084-my-colorbar-exponent-mark-is-located-at-bottom). has this changed? I wasn't able to find anything in the colorbar properties.
As you can see from the attached image, only 1 of my subplots puts this on the lower portion of the colorbar, with the red arrow showing the location I would like it.
Thanks for your help!

Accepted Answer

Chandler Hall
Chandler Hall on 13 Nov 2022
Edited: Chandler Hall on 14 Nov 2022
Your recommendation led me to this article about undocumented properties. In particular, we are interested in the SecondaryLabel field of the Ruler field of the colormap object associated with any given axis. You may notice that that the article references a property PlaceSecondaryLabelAtLowerLimit, however this property seems to have been removed. You can check all hidden properties of ruler objects in your version of Matlab with:
c = colorbar
struct(c.Ruler)
We will modify the position of the SecondaryLabel manually instead.
for i = 1:2
subplot(1, 2, i)
contourf(peaks.*1e8)
c = colorbar;
% there is a bug with accessing the ruler before rendering has finished
drawnow
c.Ruler.SecondaryLabel.Units = 'normalized';
% -0.03 to force at the lower limit, 1.03 to force at the upper limit
c.Ruler.SecondaryLabel.Position = [1 (i-1)+0.03*(-1)^i];
end
The location of the exponent seems to be naturally skewed down if the data is centered below zero. The upper placement suffers most from this since the margin between it and the top tickmark is generally much smaller. If you do force upper placement, you may have to increase the percent offset from 0.03 to 0.08 if the data is centered below zero. You'll need to set this property for each colorbar/axis individually in any case.
Consider the option of centering it:
contourf(peaks.*1e8)
c = colorbar;
drawnow
c.Ruler.SecondaryLabel.Units = 'normalized';
c.Ruler.SecondaryLabel.Position = [3 0.5];
  2 Comments
john carr
john carr on 14 Nov 2022
This was extremely helpful! you are exactly correct about using the struct to view all the hidden properties and then adjusting the SecondaryLabel.Position
Thank you so much for for help!
MAGAN SINGH
MAGAN SINGH on 26 Feb 2024
Thank you. I also faced the same problem.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!