Finding nice color bar limits automatically
조회 수: 5 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!