How do I label my y-axes with a percent sign (%) in MATLAB?
33 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Commented: Ron Fredericks
on 28 May 2020
I would like to change the axis tick labes to a percentatge of a number rather than just that set of numbers.
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
This can be done after plotting your figure. The code given below shows an example on how to get the current axis labels and format them into a string with the percent symbol. Once this is done it sets the axis tick labels back to the figure axis.
surf(peaks)
% lines 6,7,8 get the axis labels.
% lines 10,11,12 create the respective Percent vectors
% lines 14,15,16 concatenate the vectors
xticks = [get(gca,'xtick')]'; % Gets the axis tick labels.
yticks = [get(gca,'ytick')]'; % There is a transpose operation here.
zticks = [get(gca,'ztick')]'; %
percentsx = repmat('%', length(xticks),1); % makes a matrix
percentsy = repmat('%', length(yticks),1); % equal to the size
percentsz = repmat('%', length(zticks),1); % of the number of ticks
xticklabel = [num2str(xticks) percentsx];
yticklabel = [num2str(yticks) percentsy]; % concatenates the tick labels
zticklabel = [num2str(zticks) percentsz]; %with percentages
set(gca,'xticklabel',xticklabel)
set(gca,'yticklabel',yticklabel)% Sets tick labels back on the Axis
set(gca,'zticklabel',zticklabel)
% Sets them back on the Axis
1 Comment
Ron Fredericks
on 28 May 2020
xticks, yticks, zticks variable assignment do not need the brackets [ ].
But the proceedure itself works great.
More Answers (0)
See Also
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!