I have a plot that use log2
for x-axis. Now I want to get rid of xticklabels
such as 1.41421, 2.82843, etc., and add 28
to the xticklabels
. Namely, I would like to have xticklabels
as [1, 2, 4, 8, 16, 28, 32]
. Note that the constraint is to use log2
instead of original datapoints for x-axis because I want to somehow show linear speedup. How to do that?
compTime_1by1 = [1088.43, 603.71, 354.15, 236.48, 180.46, 159.06];
compTime_2by1 = [2196.49, 1179.91, 648.38, 413.79, 299.63, 268.45];
compTime_3by1 = [3238.68, 1729.27, 930.70, 590.57, 419.56, 337.15];
speedup_1by1 = compTime_1by1(1) ./ compTime_1by1;
speedup_2by1 = compTime_2by1(1) ./ compTime_2by1;
speedup_3by1 = compTime_3by1(1) ./ compTime_3by1;
numberOfCPUCores = [1, 2, 4, 8, 16, 28];
h1 = plot(log2(numberOfCPUCores), speedup_1by1, '-ob');
set(h1, 'MarkerFaceColor', get(h1,'Color'));
hold on;
h2 = plot(log2(numberOfCPUCores), speedup_2by1, '-or');
set(h2, 'MarkerFaceColor', get(h2,'Color'));
hold on;
h3 = plot(log2(numberOfCPUCores), speedup_3by1, '-ok');
set(h3, 'MarkerFaceColor', get(h3,'Color'));
xt = get(gca, 'XTick');
set(gca, 'XTickLabel', 2.^xt);
set(gca, 'FontSize', 14);
title('Parallel Scalability Test');
legend('Aspect ratio = 1 : 1', 'Aspect ratio = 2 : 1', 'Aspect ratio = 3 : 1', 'Location', 'best');
xlabel('Number of CPU cores');
ylabel('Speed-up');
grid on; grid minor;
CodePudding user response:
Instead of plotting against the logarithm of x, use semilogx
. This will scale the axis logarithmically, and its tick marks will retain the actual x values. You can then use xticks
to specify your preferred tick mark location (e.g. at powers of two instead of powers of 10).