MATLAB Answers

Label x-axes by fraction

24 views (last 30 days)
Astrik
Astrik on 30 Nov 2016
Edited: dpb on 30 Nov 2016
I am trying to graph a simple plot. The y-axis should be a vector of 19*1 with different values. My x-axis should be the numbers from 1/20 to 1/2 in ascending order. I would like to lable them as '1/2', '1/3'...'1/20'. How can I do that? I used the code
ax=gca
ax.XTick=[flip(1./(2:20))]
ax.XTickLabel={'1/2','1/3','1/4','1/5','1/6','1/7','1/8','1/9','1/10','1/11','1/12','1/13',...;
'1/14','1/15','1/16','1/17','1/18','1/19','1/20'}
plot(ax.XTick,FE(:,5)) or plot(ax.XTickLabel,FE(:,5))
I do not know how to plot using the ax.XTick,ax.XTickLabel and my 19*1 vector that is in my workspace. Any help will be appreciated.

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 30 Nov 2016
Edited: dpb on 30 Nov 2016
Plot first, then set axis properties to other than default ones...
XTicks=flip(1./(2:20));
XTickLabel=flip({'1/2','1/3','1/4','1/5','1/6','1/7','1/8','1/9','1/10','1/11','1/12','1/13',...;
'1/14','1/15','1/16','1/17','1/18','1/19','1/20'});
plot(XTicks,FE(:,5))
ax.XTick=XTicks
ax.XTickLabel=XTickLabel
Must flip the labels to match the order of the tick array.
Also, NB: Undoubtedly you'll find that the tick marks at the LH edge of the axis will be too close together to be able to have every one labeled--you'll have to pick a subset of every 2nd or 3rd for those until the spacing is large enough they don't overlap.
Also NB(2): :)
You can generate the labels programmatically rather than having to write the string out manually...
XTickLabels=num2str([2:20].','1/%d');
Note here that the vector must be a column vector to put the individual elements into rows in the text array; otherwise they'll all be run together on a long line that won't work well at all...

  0 Comments

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!