How to scale values in y-axis to be 1-100%?

조회 수: 31 (최근 30일)
Thanathip Boonmee
Thanathip Boonmee 2020년 5월 7일
답변: Steven Lord 2024년 10월 16일
The code below is what I have tried to do. I want to scale the value 200 to become 100% in the plot and the value 100 to become 50%. The x and y values are just made up, I actually need to plot 1440 different values. Help needed! Thanks in advance!
clear
x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [5, 50, 200, 180, 100, 60, 53, 0, 2];
figure
ax = axes;
plot(x,y);
set(ax, 'YTick', [0:10:100], 'YLim', [0, max(y)]);
ytickformat(ax, 'percentage');
ax.YGrid = 'on';
xlabel('Time (Minutes)');
ylabel('Energy Percentage');

채택된 답변

Mehmed Saad
Mehmed Saad 2020년 5월 7일
편집: Mehmed Saad 2020년 5월 7일
clear
x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [5, 50, 200, 180, 100, 60, 53, 0, 2];
figure
ax = axes;
plot(x,y);
set(ax, 'YTick', [0:10:100], 'YLim', [0, max(y)]);
ytickformat(ax, 'percentage');
Save the yticklabels to a variable as it might be difficult for you to create them
ylbs = ax.YTickLabel;
Set yticks to 0:20:200
ax.YTick = 0:20:200;
and label them from 0 to 100
ax.YTickLabel = ylbs;
ax.YGrid = 'on';
xlabel('Time (Minutes)');
ylabel('Energy Percentage');
Or other way of generating tick label by yourself is
generate number from 0 to 100, convert it to string, split it to cell and add a percentage sign using strcat,
clear
x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [5, 50, 200, 180, 100, 60, 53, 0, 2];
figure
ax = axes;
plot(x,y);
set(ax, 'YTick', 0:20:200,...
'YTickLabel',strcat(split(num2str(0:10:100)),'%'),...
'YLim', [0, max(y)]);
ax.YGrid = 'on';
xlabel('Time (Minutes)');
ylabel('Energy Percentage');
  댓글 수: 8
Manish
Manish 2024년 10월 16일
How to plot multiple variables with different range of values with same X axis on a single plot with Y axis as percentage?
DGM
DGM 2024년 10월 16일
If they all have a different range of values, then what does "percentage" mean? Percentage of what? If I have two y-data vectors:
y1 = [123 467 783];
y2 = [72 93 21];
What are these in %? In other words, what is the normalizing value? Do both y1 and y2 use the same normalizing value?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2024년 10월 16일
Use normalize to normalize your data to the range [0, 100].
x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [5, 50, 200, 180, 100, 60, 53, 0, 2];
yn = normalize(y, "Range", [0 100]);
plot(x, yn);
yticks(0:10:100)
ytickformat('percentage');
xlabel('Time (Minutes)');
ylabel('Energy Percentage');

카테고리

Help CenterFile Exchange에서 Axes Appearance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by