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 Center および File ExchangeAxis Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by