Double y-axis with synchronized to each other

17 vues (au cours des 30 derniers jours)
Muhammad Usman Gul
Muhammad Usman Gul le 20 Juin 2021
Suppose I have two vectors A and B. Vector A has ten values from 0 to 1, and vector B has ten values from 0 to 8. The Y-axis size is different in both vectors; however, the x-axis is the same in both vectors. Currently, I can plot a dual y-axis with A on the right and B on the left axis.
When I tried to change the left y-axis from 0 to 800 (as it is a percentage) instead of 0 to 8, the right y-axis curve looks like a zero straight line on the x-axis because it has small values compared to the left y-axis.
Can anyone guide me on how I could stretch the B curve (from 0 to 800) on the left y-axis and synchronize it with the same right y-axis (0 to 1)? I want to compare the A curve with B (in percentage) curve. Hopefully, you understand my question.

Réponse acceptée

Image Analyst
Image Analyst le 20 Juin 2021
Is this what you want?
x = 1 : 10;
A = rand(1, 10);
B = rand(1, 10) * 8
% Convert B to percent.
Bpct = B * 100;
yyaxis left
plot(x, Bpct, '.-', 'LineWidth', 3, 'MarkerSize', 30)
ylabel('B', 'fontSize', 20);
ytickformat('percentage')
yyaxis right
plot(x, A, '.-', 'LineWidth', 3, 'MarkerSize', 30)
ylabel('A', 'fontSize', 20);
grid on;
  1 commentaire
Muhammad Usman Gul
Muhammad Usman Gul le 20 Juin 2021
Thank you,
I know the previous answer is also correct. But now, it has been clear to me much more. Now everything is clear. With the help of your guidance, I have obtained the plot in my work, as I requested.

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 20 Juin 2021
You can scale the percentile axis from [0 0.8] to match the [0 8] range on the other axes; if you scale to percentages, then you'll need to plot 100*Y instead of just Y to use the built in scaling/ticks on the axes.
If, for some reason, you can't (don't see why that could be, but hypothetically) use 100*Y on the plot, then you still must use the actual data range for ylim to match the data, but you can then write
yyaxis left
yticklabels(compose('%g%%',100*yticks))
to display the tick labels as percentages. If you do it this way, however, you'll need to put it into a callback function for it to be automatically updated if/when the limits on the axis are changed as this breaks the link between the tick values and tick labels.
It's far simpler to just scale the data before plotting; you can then set
ytickformat('%g%%')
to display the labels as percentages and they'll update automagically.
  1 commentaire
Muhammad Usman Gul
Muhammad Usman Gul le 20 Juin 2021
Thanks for your prompt reply.
I have already tried to multiply by 100 on the left y-axis (B vector), but the value of the right y-axis (A vector) seems too small (like zero curves). For detail, I have shared with you some figures as an example.

Connectez-vous pour commenter.

Catégories

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

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by