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

40 vues (au cours des 30 derniers jours)
Thanathip Boonmee
Thanathip Boonmee le 7 Mai 2020
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');

Réponse acceptée

Mehmed Saad
Mehmed Saad le 7 Mai 2020
Modifié(e) : Mehmed Saad le 7 Mai 2020
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');
  6 commentaires
Mehmed Saad
Mehmed Saad le 8 Mai 2020
it is possible but will make the yaxis messy
This is another way to do what you want. see yyaxis for help
Thanathip Boonmee
Thanathip Boonmee le 10 Mai 2020
Thank you so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by