How do I change my Y-axis (or X-axis) values to percentage units and have these changes reflected on the axis?

371 次查看(过去 30 天)
For some plot, say PLOT(0:.1:1), the y-axis tick marks are specified without units at each tick mark.
I would like to change the existing ticks from 0 through 1 to 0% to 100% and have the '%' sign shown on the axis.

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2019-6-24
If you would like to merely add a percentage sign ('%') to your tick labels, without changing the scaling of the labels (ex. 1 ~ 1%, 100 ~ 100%), you can use built-in functions and/or properties of axis objects as of MATLAB R2015b.
If you are using MATLAB R2016b or a later release, you can use the "xtickformat", "ytickformat" and "ztickformat" functions to add '%' signs to your tick labels. Example:
>> plot(1:100)
>> ytickformat('percentage')
Please see the documentation page for "ytickformat" to view the alternative formatting options which are available:
If you are using MATLAB R2015b or R2016a, you can instead use the "TickLabelFormat" property of an axis to add a '%' sign. Example:
>> p = plot(1:100)
>> a = ancestor(p, 'axes')
>> a.YAxis.TickLabelFormat = '%g%%'
Please see the following documentation page for more information about the "TickLabelFormat" property:
If you would like to also change the scaling of your labels, such that 0.1 ~ 10% and 1 ~ 100%, there is currently no built-in function or property to do this. However, the units can be changed by scaling and appending the '%' sign onto the existing labels and applying the changes to the axis property "yticklabel" or "xticklabel". Example:
% Create an initial plot
plot(0:.1:1)
% Convert y-axis values to percentage values by multiplication
a=[cellstr(num2str(get(gca,'ytick')'*100))];
% Create a vector of '%' signs
pct = char(ones(size(a,1),1)*'%');
% Append the '%' signs after the percentage values
new_yticks = [char(a),pct];
% 'Reflect the changes on the plot
set(gca,'yticklabel',new_yticks)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Formatting and Annotation 的更多信息

标签

产品


版本

尚未输入任何版本。

Community Treasure Hunt

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

Start Hunting!

Translated by