How to plot log-scale with number ?

조회 수: 37 (최근 30일)
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa 2020년 6월 7일
댓글: Jeevan Kumar Bodaballa 2020년 6월 8일
How can I plot log scale with the numbers like below image ?

채택된 답변

Tommy
Tommy 2020년 6월 8일
Maybe something like this?
% data:
X = 2*logspace(1, 3, 100);
Y = rand(100,1);
% labels to keep:
labels = [20 30 50 70 100 200 300 500 1000 2000];
% prepare axes, plot:
ax = axes('XScale', 'log', 'NextPlot', 'add');
plot(ax, X, Y, '.')
ax.XLim = [20 2000];
ax.XAxis.TickDirection = 'out';
% steal the tick locations from the minor ticks, then turn the minor ticks off:
drawnow
ax.XTick = ax.XAxis.MinorTickValues;
ax.XMinorTick = 'off';
% remove labels we don't want to keep:
ax.XTickLabel(~ismember(ax.XTick, labels)) = {''};
  댓글 수: 1
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa 2020년 6월 8일
This is working but I'd wonder if we have any optmised code !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2020년 6월 8일
One way is to use the semilogx function.
  댓글 수: 3
the cyclist
the cyclist 2020년 6월 8일
There are multiple ways to do this task. Here is one way:
% data:
X = 2*logspace(1, 3, 100);
Y = rand(100,1);
% Locations of the ticks (and also how we want them labeled)
tickLoc = [20 30 50 70 100 200 300 500 1000 2000];
% Create a new figure window, and plot the data with log x-axis
figure
semilogx(X,Y);
% Set the axis limit, tick locations and labels
set(gca, ...
'XLim',[20 2000], ...
'XTick', tickLoc, ...
'XTickLabel',tickLoc);
Tommy's solution and mine are doing effectively the same thing (although I did not do all the little things he did like make the tick marks point outward). The biggest difference is that I used the semilogx command, where he created the axes first, and then set the 'XScale' property to 'log'.
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa 2020년 6월 8일
Appreciated for your help and explanation. :)
Thank you

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by