# Finding nice color bar limits automatically

5 ビュー (過去 30 日間)
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 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 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 2024 年 1 月 22 日
good idea, I made that change as well

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by