tags:

views:

68

answers:

3

I want to have the x-axis logarithmic to the base 2: From 2^10 to 2^25 and at each step the exponent should increase by one, while the y-axis should be linear.

How is this possible? I already figured out

set(gca,'XScale','log')

but u can't set the base.

A: 

Something like semilogx() ??

http://www.mathworks.com/help/techdoc/ref/semilogx.html

ysap
"semilogx(Y) creates a plot using a base 10 logarithmic scale for the x-axis and a linear scale for the y-axis." No word on how the base can be changed.
Nils
Yes, you are right. I missed the base 2 point.
ysap
Just in case you are not aware - are you OK with doing explicit base conversion for the plot? You can take the log2() and use linear plot() instead. Only problem is that the X tick labels will not be your data but its logarithm.
ysap
+6  A: 

You can plot directly using the plot command

plot (log2(x), y)

but then your x ticks will be the logarithm rather than the actual value. You could either just change your label

xlabel('Log (base 2) of quantity X');

or you can redo the ticks manually.

xt = get(gca, 'XTick');
set (gca, 'XTickLabel', 2.^xt);

Or you can be really fancy

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

which will evenly space the tick marks on the log scale, and label them according to their power of 2

Marc
I wanted to use LaTex to change the tick labels to '$2^{10}' etc., but I can't figure out how to get matlab to intepret tick labels using latex
Marc
so, there is a package on the exchange that will let you do latex tick labels if you're really ambitious. I haven't tried it out. http://www.mathworks.se/matlabcentral/fileexchange/23841-format-tick-labels-extended
Marc
+5  A: 

Consider this example:

%# 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

alt text

Amro

related questions