필터 지우기
필터 지우기

Labeling Minor Tick Marks in Plots

조회 수: 37 (최근 30일)
Abed Alnaif
Abed Alnaif 2011년 10월 6일
Hi, I am trying (without success) to find a way to automatically label the minor tick marks in a plot -- specifically, a semi-log-y plot. I thought that I would be able to do this using the get(gca,...) command to obtain the locations of the minor tick marks, and then the set(gca,'YTick',...) command to set the minor ticks as major ticks. However, I wasn't able to figure out how to obtain the locations of the minor ticks using the get(gca,...) command. Please let me know if you have a solution. An example code is shown below:
function label_minor_ticks()
figure
y = [0.4,0.5,0.8,1,1.5,2,4];
semilogy(y);
end

채택된 답변

Grzegorz Knor
Grzegorz Knor 2011년 10월 6일
Remove YMinorTicks, and add new YTicks:
y = [0.4,0.5,0.8,1,1.5,2,4];
semilogy(y);
set(gca,'yMinorTick','off')
yRange = get(gca,'ylim');
p = ceil(log(yRange) / log(10));%nextpow10
ticks = [];
for k=p(1):p(2)
if k==p(1)
ticks = [ticks yRange(1):10^(k-1):10^k];
elseif k==p(2)
ticks = [ticks 10^(k-1)+10^(k-1):10^(k-1):yRange(2)];
else
ticks = [ticks 10^(k-1)+10^(k-1):10^(k-1):10^(k-1)];
end
end
set(gca,'ytick',ticks(2:end-1))
This loop does not look good for me, but it works :)

추가 답변 (1개)

Abed Alnaif
Abed Alnaif 2011년 10월 6일
Thanks! I thought that there would be an easier way. I noticed one minor error in your solution, and I made a few other minor changes:
figure
y = [0.32,0.4,0.5,0.8,1,1.5,2,4,4.5,9.9,10.5];
semilogy(y);
set(gca,'yMinorTick','off')
min_y = min(y);
max_y = max(y);
most_sig_position_min = 10^floor((log10(min_y)));
most_sig_digit_min = floor(min_y / most_sig_position_min);
min_yaxis = most_sig_digit_min * most_sig_position_min;
most_sig_position_max = 10^floor((log10(max_y)));
most_sig_digit_max = ceil(max_y / most_sig_position_max);
max_yaxis = most_sig_digit_max * most_sig_position_max;
p(1) = ceil(log10(min_yaxis));
p(2) = ceil(log10(max_yaxis));
ticks = [];
for k=p(1):p(2)
if k==p(1)
ticks = [ticks min_yaxis:10^(k-1):10^k];
elseif k==p(2)
ticks = [ticks 10^(k-1)+10^(k-1):10^(k-1):max_yaxis];
else
ticks = [ticks 10^(k-1)+10^(k-1):10^(k-1):10^k];
end
end
set(gca,'ytick',ticks())
axis([-Inf Inf min_yaxis max_yaxis])

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by