Why won't my x-axis tick label format change?
37 次查看(过去 30 天)
显示 更早的评论
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';
0 个评论
采纳的回答
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 个评论
Star Strider
2020-2-15
As always, my pleasure!
The 'significant' option is relatively new (I believe R2016b).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!