# Finding nice color bar limits automatically

5 visualizzazioni (ultimi 30 giorni)
William Thielicke il 21 Gen 2024
Commentato: David Goodmanson il 22 Gen 2024
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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

David Goodmanson il 21 Gen 2024
Modificato: David Goodmanson il 21 Gen 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 CommentiMostra NessunoNascondi Nessuno
William Thielicke il 21 Gen 2024
When zero is the input for x, the result may not be finite. I simply added a check for this.
David Goodmanson il 22 Gen 2024
good idea, I made that change as well

Accedi per commentare.

### Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by