【问题标题】:Matlab plot with partly exponential or logarithmic scale depending on the y values取决于 y 值的具有部分指数或对数刻度的 Matlab 图
【发布时间】:2017-08-31 08:27:56
【问题描述】:

我想在 Matlab 中做一个简单的绘图,比如说

% data:
x = -9:8;
% dates:
Y = [];
for year = 2008:2016
    Y = vertcat(Y,[year;year]);
end
M = repmat([01;06],9,1);
D = [01];
vector = datetime(Y,M,D);

plot(vector, x);
dateaxis('x', 12);

现在我想要所有 x 值的对数刻度

x<0

或指数级

x>0

但情节的其他部分的正常比例。原因是负值下降得如此之低,以至于在正常比例下,正值似乎全为零。我查看了帮助,但半对数函数等对我没有帮助。有什么建议吗?

【问题讨论】:

    标签: matlab plot matlab-figure


    【解决方案1】:

    您可以创建两个子图并将它们放在一起

    % plotting
    figure;
    p1 = subplot(2,1,1);
    idx = x>=0; plot(vector(idx), x(idx));
    p2 = subplot(2,1,2);
    idx = x<=0; plot(vector(idx), x(idx));
    % Make x-axis limits the same (must use datenum)
    lims = datenum([min(vector), max(vector)]);
    xlim(p1, lims); xlim(p2, lims);
    % Make the plots meet in the middle
    h = 0.45; w = 0.9; % height and width
    set(p1, 'position', [(1-w)/2, 0.5, w, h])
    set(p2, 'position', [(1-w)/2, 0.5-h, w, h])
    % Ensure the y axes meet at 0
    ylim(p1, [0, max(x)]); ylim(p2, [min(x), 0]);
    

    这两个单独的情节可以按照你喜欢的方式完成。因此,如果您使用相关方法绘制它们,您将得到一个具有指数 y 轴和一个具有对数 y 轴的方法。

    您可以使用而不是上面的 plot(...)

    % log y plot
    semilogy(datenum(vector(idx)), x(idx))
    

    请注意,此操作的输出完全符合预期,但您尝试执行的实际绘图听起来非常混乱。如果轴确实想要不同,在大多数情况下将其呈现为两个单独的图可能会更好。在这种情况下,请使用上面没有position 行的代码!


    在弄乱绘图类型之前,这是输出的样子,0 线上方和下方的 y 轴是完全独立的,因为这实际上是 2 个绘图:

    【讨论】:

    • 谢谢!它工作正常,但我想我同意它看起来令人困惑;)所以我将使用 2 个地块,一个具有正常比例,一个具有对数比例。现在我知道如何用日期进行符号学了,我唯一的问题是:“不显示负值”。你有什么建议?
    【解决方案2】:

    当你绘制(x)时,你正在绘制的是:

    plot(1:length(x),x) 
    

    假设你有另一个向量:

    t= 1:length(x)
    

    现在你可以操纵这个向量来获得水平轴上的任何缩放,例如:

    t(1:10) = exp(-t(1:10));
    

    然后,如果您绘制 (t,x),您将以指数比例绘制前 10 个元素!

    【讨论】:

    • 不幸的是更复杂,因为我的向量是日期...我编辑了上面的问题以使其更精确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2014-01-08
    • 2020-06-20
    • 2012-04-24
    相关资源
    最近更新 更多