Finding nice color bar limits automatically

5 views (last 30 days)
Dear all, I have a plot with a colorbar. The limits of the color bar can't be adjusted automatically, because the plot is a bit complicated with RGB conversion etc. So I want to apply my own automatic upper and lower limit to the color bar. These limits should "look nice": if the data ranges e.g. from -0.06535 to 7.6682, the limits should be something like -0.1 and 8 (or 7.9...?). If the data ranges from 21 to 493 it should range from 20 to 500 or so. If the data ranges from 0.00653 to 0.0954 the limits should e.g. be 0.005 and 0.01. You probably noticed that I have difficulties to define what "looks nice", do you have a suggestion for my problem? Thanks a lot!

Accepted Answer

David Goodmanson
David Goodmanson on 21 Jan 2024
Edited: David Goodmanson on 21 Jan 2024
Hi William,
It looks like you want to move from the limits you have to some with fewer significant figures such as 1 or 2. Here is one possible answer with some functions to try.
function y = roundn(x,n)
% round x to n significant figures.
% rounds toward nearest integer in last significant figure.
% y = roundn(x,n)
logflr = floor(log10(abs(x)));
pof10 = 10.^(n-1-logflr);
y = round(x.*pof10)./pof10;
function y = fixn(x,n)
% round x to n significant figures.
% rounds towards zero.
% y = fixn(x,n)
logflr = floor(log10(abs(x)));
pof10 = 10.^(n-1-logflr);
y = fix(x.*pof10)./pof10;
function y = floorn(x,n)
% round x to n significant figures.
% rounds toward minus inf.
% y = floorn(x,n)
logflr = floor(log10(abs(x)));
pof10 = 10.^(n-1-logflr);
y = floor(x.*pof10)./pof10;
function y = ceiln(x,n)
% round x to n significant figures.
% rounds toward plus inf.
% y = ceiln(x,n)
logflr = floor(log10(abs(x)));
pof10 = 10.^(n-1-logflr);
y = ceil(x.*pof10)./pof10;
  2 Comments
William Thielicke
William Thielicke on 21 Jan 2024
When zero is the input for x, the result may not be finite. I simply added a check for this.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!