正面与正面Matlab中的负Log10比例Y轴 [英] Positive & Negitive Log10 Scale Y axis in Matlab
问题描述
我遇到了一个问题,我的数据集的范围是-10 ^ 3到10 ^ 3
Hi i'm having a problem where I have a dataset which ranges between -10^3 to 10^3
我需要能够以对数刻度来绘制此图形,但是符号学不能绘制负值
I need to be able to plot this as with a log scale but semilogy cannot plot negative values
例如,我的数据是:
x = [-3,-2,-1,0,1,2,3];
y = [-1000,-100,-10,1,10,100,1000];
(或通常为y=sign(x).*10.^abs(x);
)
如何在MATLAB中以对数标度绘制此图?如果可能的话,对数刻度刻度也可以在Y轴上也很好
How can I plot this in MATLAB with a log scale? If possible It would be great if the log scale ticks could be on the Y-axis too
推荐答案
使用您的实际数据作为标签,但是使用log10
缩放绘制的数据.
Use your actual data as labels, but scale the plotted 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
对于N = 7
:
对于N = 11
如何为N
查找有效值?
How to find a valid value for N
?
以下函数(感谢gnovice )将返回您可以为N
选择的所有可能值:>
The following function (thanks to gnovice) will return all possible values you could choose for N
:
n = numel(x);
N = find(rem(n./(1:n), 1) == 0) + 1;
关于符号学样式的标签:通过在情节之前添加以下行:
about the semilogy-style labels: by adding the following line before the plot:
YTickLabels = cellfun(@(x) ['10^' num2str(x)], num2cell(YTick),'UniformOutput',false)
您至少可以实现以下目标: 不漂亮也不通用,但是对您来说是一个很好的起点.
you could at least achieve something like this: not beautiful and not generic, but a good point to start for you.
这篇关于正面与正面Matlab中的负Log10比例Y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!