필터 지우기
필터 지우기

Why won't my x-axis tick label format change?

조회 수: 37 (최근 30일)
Brendan Laney
Brendan Laney 2020년 2월 15일
댓글: Star Strider 2020년 2월 15일
Hello,
I have provided my code and the figure it creates in this post. I've tried everything that I know of to change the x-axis numbers on the graph. I have tried xtickformat, ax = gca, ax.XAxis.Exponent = 0 and ax.XTickLabelMode = 'manual'.
My goal is to make the x-axis tick values appear like: "10, 1, 0.1, 0.001" etc. instead of containing exponent values.
If anybody could give me guidance on where my code is causing the trouble or a function I am unaware of that can help me solve this problem it would be greatly appreciated! I am fairly new to MatLab and am really enjoying it so any tips in general to imrpove this short code is also appreciated.
% Perecent passing
y = [100 99.69 99.69 99.62 99.48 99 98.65 91.09 73.63 ...
73.28 70.09 68.50 66.91 60.53 57.35 52.57 49.38 ...
47.79 44.60 43.01 41.42];
% Particle diameter (mm)
x = [19 9.5 4.76 2 0.85 0.42 0.3 0.149 0.075 0.0698 ...
0.0391 0.0284 0.0197 0.0140 0.0105 0.0075 0.0054...
0.0038 0.0031 0.0026 0.0011];
semilogx(x,y,'linewidth',0.85)
xlim([0.001 100]);
ylim([0 100]);
ax = gca;
set(ax, 'XDir','reverse');
ytickformat('%g%%');
title('Grain Size Analysis: Clayey Soil','FontWeight','normal');
ylabel('Percent passing','FontSize',12);
xlabel('Grain size (mm)','FontSize',12);
grid
l1 = yline(73.63,'--k',{'Silt sized','particles'});
l2 = yline(42,'--k',{'Clay sized','particles'});
l1.LabelVerticalAlignment = 'middle';
l1.LabelHorizontalAlignment = 'center';
l2.LabelVerticalAlignment = 'middle';
l2.LabelHorizontalAlignment = 'center';

채택된 답변

Star Strider
Star Strider 2020년 2월 15일
Add these two lines to your code:
xt_sigfig = round(xt, 3, 'significant');
ax.XTickLabel = xt_sigfig;
so the complete code is now:
% Perecent passing
y = [100 99.69 99.69 99.62 99.48 99 98.65 91.09 73.63 ...
73.28 70.09 68.50 66.91 60.53 57.35 52.57 49.38 ...
47.79 44.60 43.01 41.42];
% Particle diameter (mm)
x = [19 9.5 4.76 2 0.85 0.42 0.3 0.149 0.075 0.0698 ...
0.0391 0.0284 0.0197 0.0140 0.0105 0.0075 0.0054...
0.0038 0.0031 0.0026 0.0011];
semilogx(x,y,'linewidth',0.85)
xlim([0.001 100]);
ylim([0 100]);
ax = gca;
set(ax, 'XDir','reverse');
ytickformat('%g%%');
title('Grain Size Analysis: Clayey Soil','FontWeight','normal');
ylabel('Percent passing','FontSize',12);
xlabel('Grain size (mm)','FontSize',12);
xt = ax.XTick;
xt_sigfig = round(xt, 3, 'significant');
ax.XTickLabel = xt_sigfig;
grid
l1 = yline(73.63,'--k',{'Silt sized','particles'});
l2 = yline(42,'--k',{'Clay sized','particles'});
l1.LabelVerticalAlignment = 'middle';
l1.LabelHorizontalAlignment = 'center';
l2.LabelVerticalAlignment = 'middle';
l2.LabelHorizontalAlignment = 'center';
to produce this plot:
The plot now has the desired x-tick label format.
  댓글 수: 2
Brendan Laney
Brendan Laney 2020년 2월 15일
Very appreciate the quick response! I had no idea that code you suggested was even an option. That will be very handy to know, thanks again.
Star Strider
Star Strider 2020년 2월 15일
As always, my pleasure!
The 'significant' option is relatively new (I believe R2016b).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by