Finding nice color bar limits automatically

조회 수: 3 (최근 30일)
William Thielicke
William Thielicke 2024년 1월 21일
댓글: David Goodmanson 2024년 1월 22일
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!

채택된 답변

David Goodmanson
David Goodmanson 2024년 1월 21일
편집: David Goodmanson 2024년 1월 21일
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
William Thielicke
William Thielicke 2024년 1월 21일
When zero is the input for x, the result may not be finite. I simply added a check for this.
David Goodmanson
David Goodmanson 2024년 1월 22일
good idea, I made that change as well

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by