绘制日志基数2的比例

时间:2015-09-25 17:05:17

标签: matlab

按照此question中的建议,我尝试绘制数据的log2值,并在水平和垂直轴上显示刻度标签,作为2的幂。但是我没有得到所需的刻度沿任一轴的标签。在两个轴上,我希望刻度标签显示为2的整数幂,即在x轴上:3和4,在y轴上:0,-1,-2,-3, - 4和-5。这是我的代码:

x = [ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
Tcomm = [ 0.0357, 0.0515, 0.0745, 0.0956, 0.122, 0.1596, 0.2005, 0.2443, 0.2873, 0.3752, 0.4148, 0.5102, 0.5882 ]
plot(log2(x), log2(Tcomm), 'ko')
set(gca, 'XTickLabel',[])                      %# suppress current x-labels
xlim([log2(8),log2(16)])
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

xlabel('N');
ylim([log2(0.0357), log2(0.58825)])
set(gca, 'YTickLabel',[])
xt = get(gca, 'YTick');
yl = get(gca, 'XLim');
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','middle', ...             %# v-align to be underneath
'HorizontalAlignment','right');           %# h-aligh to be centered

ylabel('CommunicationTime');
title('(a)');

1 个答案:

答案 0 :(得分:0)

对于Y轴部分,在text命令中,只需交换第一个和第二个值:

xlabel('N');
ylim([log2(0.0357), log2(0.58825)])
set(gca, 'YTickLabel',[])
xt = get(gca, 'YTick');
yl = get(gca, 'XLim');
str = cellstr( num2str(xt(:),'2^{%d}') );      %# format x-ticks as 2^{xx}
hTxt = text( yl(ones(size(xt))),xt, str, ...   %# create text at same locations
'Interpreter','tex', ...                   %# specify tex interpreter
'VerticalAlignment','middle', ...             %# v-align to be underneath
'HorizontalAlignment','right');           %# h-aligh to be centered