How do I change my Y-axis (or X-axis) values to percentage units and have these changes reflected on the axis?
조회 수: 347 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2010년 8월 25일
편집: MathWorks Support Team
2019년 6월 24일
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
2019년 6월 25일
편집: 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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Formatting and Annotation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!