在matlab中为对数图选择不同的基数

Nil*_*ils 15 matlab

我希望x轴与对数2成对数:从2 ^ 10到2 ^ 25,并且在每一步,指数应该增加1,而y轴应该是线性的.

这怎么可能?我已经想通了

set(gca,'XScale','log')
Run Code Online (Sandbox Code Playgroud)

但你不能设置基数.

Amr*_*mro 17

考虑这个例子:

%# some random data
x = 2.^(0:10);
y = rand(size(x));

plot(log2(x), y)                               %# plot on log2 x-scale
set(gca, 'XTickLabel',[])                      %# suppress current x-labels

xt = get(gca, 'XTick');
yl = get(gca, 'YLim');
str = cellstr( num2str(xt(:),'2^{%d}') );      %# format x-ticks as 2^{xx}
hTxt = text(xt, yl(ones(size(xt))), str, ...   %# create text at same locations
    'Interpreter','tex', ...                   %# specify tex interpreter
    'VerticalAlignment','top', ...             %# v-align to be underneath
    'HorizontalAlignment','center');           %# h-aligh to be centered
Run Code Online (Sandbox Code Playgroud)

替代文字


Mar*_*arc 15

您可以使用plot命令直接绘图

plot (log2(x), y)
Run Code Online (Sandbox Code Playgroud)

但是你的x刻度将是对数而不是实际值.你可以改变你的标签

xlabel('Log (base 2) of quantity X');
Run Code Online (Sandbox Code Playgroud)

或者你可以手动重做滴答.

xt = get(gca, 'XTick');
set (gca, 'XTickLabel', 2.^xt);
Run Code Online (Sandbox Code Playgroud)

或者你真的很喜欢

xticks = 10:25;
set(gca, 'XTick', xticks);
for j = 1:length(xticks)
  xtl{j} = ['2^' num2str(xticks(j))];
end
set(gca, 'XTickLabel', xtl)
Run Code Online (Sandbox Code Playgroud)

它将在对数刻度上均匀地划分刻度线,并根据它们的2的幂来标记它们