Positive and Negative Log10 Scaling Y-axis in Matlab

Hi, I have a problem when I have a dataset that is in the range of -10 ^ 3 to 10 ^ 3

I need to be able to build this as a logarithm scale, but semilogy cannot build negative values

Let's say, for example, my data:

x = [-3,-2,-1,0,1,2,3];
y = [-1000,-100,-10,1,10,100,1000];

(or in general y=sign(x).*10.^abs(x);)

How can I build this in MATLAB with a log scale? If possible, it would be great if tick ticks were also on the Y axis.

+4
source share
3 answers

Use your actual data as labels, but scale the resulting data with log10.

% data
x = -3:0.1:3;
y = sign(x).*10.^abs(x);

% scaling function
scale = @(x) sign(x).*log10(abs(x));

N = 7;    % number of ticks desired

% picking of adequate values for the labels
TickMask = linspace(1,numel(y),N);
YTickLabels = y(TickMask);

% scale labels and plotdata, remove NaN ->inconsistency, do you really want that?
YTick = scale( YTickLabels );
Y = scale(y);

YTick(isnan(YTick)) = 0;
Y(isnan(Y)) = 0;

% plot
plot(x,Y)
set(gca,'YTick',YTick,'YTickLabels',YTickLabels)
grid on

For N = 7:

enter image description here

For N = 11

enter image description here


N?

( gnovice) , N:

n = numel(x);
N = find(rem(n./(1:n), 1) == 0) + 1;

: :

YTickLabels = cellfun(@(x) ['10^' num2str(x)], num2cell(YTick),'UniformOutput',false)

- : enter image description here , .

+4

, , , , ! , . 100 - 10 - 1 - 1/10 - 1/100 -..., , .

+1

:

x=logspace(-3,3);
y=sign(x).*10.^abs(x);
loglog(x,y)

enter image description here

0

All Articles