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

354 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 25 de Jun. de 2019
Editada: MathWorks Support Team el 24 de Jun. de 2019
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)

Más respuestas (0)

Categorías

Más información sobre Formatting and Annotation en Help Center y File Exchange.

Etiquetas

Productos


Versión

Aún no se ha introducido una versión.

Community Treasure Hunt

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

Start Hunting!

Translated by